{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# \u7b2c\u4e94\u7ae0\uff1a\u6587\u4ef6\u4e0eIO\n \u6240\u6709\u7a0b\u5e8f\u90fd\u8981\u5904\u7406\u8f93\u5165\u548c\u8f93\u51fa\u3002\n\u8fd9\u4e00\u7ae0\u5c06\u6db5\u76d6\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u6587\u4ef6\uff0c\u5305\u62ec\u6587\u672c\u548c\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u6587\u4ef6\u7f16\u7801\u548c\u5176\u4ed6\u76f8\u5173\u7684\u5185\u5bb9\u3002\n\u5bf9\u6587\u4ef6\u540d\u548c\u76ee\u5f55\u7684\u64cd\u4f5c\u4e5f\u4f1a\u6d89\u53ca\u5230\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.1 \u8bfb\u5199\u6587\u672c\u6570\u636e\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u9700\u8981\u8bfb\u5199\u5404\u79cd\u4e0d\u540c\u7f16\u7801\u7684\u6587\u672c\u6570\u636e\uff0c\u6bd4\u5982ASCII\uff0cUTF-8\u6216UTF-16\u7f16\u7801\u7b49\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u5e26\u6709 rt \u6a21\u5f0f\u7684 open() \u51fd\u6570\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u3002\u5982\u4e0b\u6240\u793a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Read the entire file as a single string\nwith open('somefile.txt', 'rt') as f:\n    data = f.read()\n\n# Iterate over the lines of the file\nwith open('somefile.txt', 'rt') as f:\n    for line in f:\n        # process line\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7c7b\u4f3c\u7684\uff0c\u4e3a\u4e86\u5199\u5165\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\uff0c\u4f7f\u7528\u5e26\u6709 wt \u6a21\u5f0f\u7684 open() \u51fd\u6570\uff0c\n\u5982\u679c\u4e4b\u524d\u6587\u4ef6\u5185\u5bb9\u5b58\u5728\u5219\u6e05\u9664\u5e76\u8986\u76d6\u6389\u3002\u5982\u4e0b\u6240\u793a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Write chunks of text data\nwith open('somefile.txt', 'wt') as f:\n    f.write(text1)\n    f.write(text2)\n    ...\n\n# Redirected print statement\nwith open('somefile.txt', 'wt') as f:\n    print(line1, file=f)\n    print(line2, file=f)\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u662f\u5728\u5df2\u5b58\u5728\u6587\u4ef6\u4e2d\u6dfb\u52a0\u5185\u5bb9\uff0c\u4f7f\u7528\u6a21\u5f0f\u4e3a at \u7684 open() \u51fd\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6587\u4ef6\u7684\u8bfb\u5199\u64cd\u4f5c\u9ed8\u8ba4\u4f7f\u7528\u7cfb\u7edf\u7f16\u7801\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528 sys.getdefaultencoding() \u6765\u5f97\u5230\u3002\n\u5728\u5927\u591a\u6570\u673a\u5668\u4e0a\u9762\u90fd\u662futf-8\u7f16\u7801\u3002\u5982\u679c\u4f60\u5df2\u7ecf\u77e5\u9053\u4f60\u8981\u8bfb\u5199\u7684\u6587\u672c\u662f\u5176\u4ed6\u7f16\u7801\u65b9\u5f0f\uff0c\n\u90a3\u4e48\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u53ef\u9009\u7684 encoding \u53c2\u6570\u7ed9open()\u51fd\u6570\u3002\u5982\u4e0b\u6240\u793a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with open('somefile.txt', 'rt', encoding='latin-1') as f:\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Python\u652f\u6301\u975e\u5e38\u591a\u7684\u6587\u672c\u7f16\u7801\u3002\u51e0\u4e2a\u5e38\u89c1\u7684\u7f16\u7801\u662fascii, latin-1, utf-8\u548cutf-16\u3002\n\u5728web\u5e94\u7528\u7a0b\u5e8f\u4e2d\u901a\u5e38\u90fd\u4f7f\u7528\u7684\u662fUTF-8\u3002\nascii\u5bf9\u5e94\u4eceU+0000\u5230U+007F\u8303\u56f4\u5185\u76847\u4f4d\u5b57\u7b26\u3002\nlatin-1\u662f\u5b57\u82820-255\u5230U+0000\u81f3U+00FF\u8303\u56f4\u5185Unicode\u5b57\u7b26\u7684\u76f4\u63a5\u6620\u5c04\u3002\n\u5f53\u8bfb\u53d6\u4e00\u4e2a\u672a\u77e5\u7f16\u7801\u7684\u6587\u672c\u65f6\u4f7f\u7528latin-1\u7f16\u7801\u6c38\u8fdc\u4e0d\u4f1a\u4ea7\u751f\u89e3\u7801\u9519\u8bef\u3002\n\u4f7f\u7528latin-1\u7f16\u7801\u8bfb\u53d6\u4e00\u4e2a\u6587\u4ef6\u7684\u65f6\u5019\u4e5f\u8bb8\u4e0d\u80fd\u4ea7\u751f\u5b8c\u5168\u6b63\u786e\u7684\u6587\u672c\u89e3\u7801\u6570\u636e\uff0c\n\u4f46\u662f\u5b83\u4e5f\u80fd\u4ece\u4e2d\u63d0\u53d6\u51fa\u8db3\u591f\u591a\u7684\u6709\u7528\u6570\u636e\u3002\u540c\u65f6\uff0c\u5982\u679c\u4f60\u4e4b\u540e\u5c06\u6570\u636e\u56de\u5199\u56de\u53bb\uff0c\u539f\u5148\u7684\u6570\u636e\u8fd8\u662f\u4f1a\u4fdd\u7559\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8bfb\u5199\u6587\u672c\u6587\u4ef6\u4e00\u822c\u6765\u8bb2\u662f\u6bd4\u8f83\u7b80\u5355\u7684\u3002\u4f46\u662f\u4e5f\u51e0\u70b9\u662f\u9700\u8981\u6ce8\u610f\u7684\u3002\n\u9996\u5148\uff0c\u5728\u4f8b\u5b50\u7a0b\u5e8f\u4e2d\u7684with\u8bed\u53e5\u7ed9\u88ab\u4f7f\u7528\u5230\u7684\u6587\u4ef6\u521b\u5efa\u4e86\u4e00\u4e2a\u4e0a\u4e0b\u6587\u73af\u5883\uff0c\n\u4f46 with \u63a7\u5236\u5757\u7ed3\u675f\u65f6\uff0c\u6587\u4ef6\u4f1a\u81ea\u52a8\u5173\u95ed\u3002\u4f60\u4e5f\u53ef\u4ee5\u4e0d\u4f7f\u7528 with \u8bed\u53e5\uff0c\u4f46\u662f\u8fd9\u65f6\u5019\u4f60\u5c31\u5fc5\u987b\u8bb0\u5f97\u624b\u52a8\u5173\u95ed\u6587\u4ef6\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('somefile.txt', 'rt')\ndata = f.read()\nf.close()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53e6\u5916\u4e00\u4e2a\u95ee\u9898\u662f\u5173\u4e8e\u6362\u884c\u7b26\u7684\u8bc6\u522b\u95ee\u9898\uff0c\u5728Unix\u548cWindows\u4e2d\u662f\u4e0d\u4e00\u6837\u7684(\u5206\u522b\u662f \\n \u548c \\r\\n )\u3002\n\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cPython\u4f1a\u4ee5\u7edf\u4e00\u6a21\u5f0f\u5904\u7406\u6362\u884c\u7b26\u3002\n\u8fd9\u79cd\u6a21\u5f0f\u4e0b\uff0c\u5728\u8bfb\u53d6\u6587\u672c\u7684\u65f6\u5019\uff0cPython\u53ef\u4ee5\u8bc6\u522b\u6240\u6709\u7684\u666e\u901a\u6362\u884c\u7b26\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5355\u4e2a \\n \u5b57\u7b26\u3002\n\u7c7b\u4f3c\u7684\uff0c\u5728\u8f93\u51fa\u65f6\u4f1a\u5c06\u6362\u884c\u7b26 \\n \u8f6c\u6362\u4e3a\u7cfb\u7edf\u9ed8\u8ba4\u7684\u6362\u884c\u7b26\u3002\n\u5982\u679c\u4f60\u4e0d\u5e0c\u671b\u8fd9\u79cd\u9ed8\u8ba4\u7684\u5904\u7406\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7ed9 open() \u51fd\u6570\u4f20\u5165\u53c2\u6570 newline='' \uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Read with disabled newline translation\nwith open('somefile.txt', 'rt', newline='') as f:\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u8bf4\u660e\u4e24\u8005\u4e4b\u95f4\u7684\u5dee\u5f02\uff0c\u4e0b\u9762\u6211\u5728Unix\u673a\u5668\u4e0a\u9762\u8bfb\u53d6\u4e00\u4e2aWindows\u4e0a\u9762\u7684\u6587\u672c\u6587\u4ef6\uff0c\u91cc\u9762\u7684\u5185\u5bb9\u662f hello world!\\r\\n \uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Newline translation enabled (the default)\nf = open('hello.txt', 'rt')\nf.read()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Newline translation disabled\ng = open('hello.txt', 'rt', newline='')\ng.read()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u4e00\u4e2a\u95ee\u9898\u5c31\u662f\u6587\u672c\u6587\u4ef6\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u7f16\u7801\u9519\u8bef\u3002\n\u4f46\u4f60\u8bfb\u53d6\u6216\u8005\u5199\u5165\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u4f60\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e2a\u7f16\u7801\u6216\u8005\u89e3\u7801\u9519\u8bef\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('sample.txt', 'rt', encoding='ascii')\nf.read()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u51fa\u73b0\u8fd9\u4e2a\u9519\u8bef\uff0c\u901a\u5e38\u8868\u793a\u4f60\u8bfb\u53d6\u6587\u672c\u65f6\u6307\u5b9a\u7684\u7f16\u7801\u4e0d\u6b63\u786e\u3002\n\u4f60\u6700\u597d\u4ed4\u7ec6\u9605\u8bfb\u8bf4\u660e\u5e76\u786e\u8ba4\u4f60\u7684\u6587\u4ef6\u7f16\u7801\u662f\u6b63\u786e\u7684(\u6bd4\u5982\u4f7f\u7528UTF-8\u800c\u4e0d\u662fLatin-1\u7f16\u7801\u6216\u5176\u4ed6)\u3002\n\u5982\u679c\u7f16\u7801\u9519\u8bef\u8fd8\u662f\u5b58\u5728\u7684\u8bdd\uff0c\u4f60\u53ef\u4ee5\u7ed9 open() \u51fd\u6570\u4f20\u9012\u4e00\u4e2a\u53ef\u9009\u7684 errors \u53c2\u6570\u6765\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\u3002\n\u4e0b\u9762\u662f\u4e00\u4e9b\u5904\u7406\u5e38\u89c1\u9519\u8bef\u7684\u65b9\u6cd5\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Replace bad chars with Unicode U+fffd replacement char\nf = open('sample.txt', 'rt', encoding='ascii', errors='replace')\nf.read()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Ignore bad chars entirely\ng = open('sample.txt', 'rt', encoding='ascii', errors='ignore')\ng.read()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u7ecf\u5e38\u4f7f\u7528 errors \u53c2\u6570\u6765\u5904\u7406\u7f16\u7801\u9519\u8bef\uff0c\u53ef\u80fd\u4f1a\u8ba9\u4f60\u7684\u751f\u6d3b\u53d8\u5f97\u5f88\u7cdf\u7cd5\u3002\n\u5bf9\u4e8e\u6587\u672c\u5904\u7406\u7684\u9996\u8981\u539f\u5219\u662f\u786e\u4fdd\u4f60\u603b\u662f\u4f7f\u7528\u7684\u662f\u6b63\u786e\u7f16\u7801\u3002\u5f53\u6a21\u68f1\u4e24\u53ef\u7684\u65f6\u5019\uff0c\u5c31\u4f7f\u7528\u9ed8\u8ba4\u7684\u8bbe\u7f6e(\u901a\u5e38\u90fd\u662fUTF-8)\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.2 \u6253\u5370\u8f93\u51fa\u81f3\u6587\u4ef6\u4e2d\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5c06 print() \u51fd\u6570\u7684\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u4e00\u4e2a\u6587\u4ef6\u4e2d\u53bb\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728 print() \u51fd\u6570\u4e2d\u6307\u5b9a file \u5173\u952e\u5b57\u53c2\u6570\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with open('d:/work/test.txt', 'wt') as f:\n    print('Hello World!', file=f)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5173\u4e8e\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u6587\u4ef6\u4e2d\u5c31\u8fd9\u4e9b\u4e86\u3002\u4f46\u662f\u6709\u4e00\u70b9\u8981\u6ce8\u610f\u7684\u5c31\u662f\u6587\u4ef6\u5fc5\u987b\u662f\u4ee5\u6587\u672c\u6a21\u5f0f\u6253\u5f00\u3002\n\u5982\u679c\u6587\u4ef6\u662f\u4e8c\u8fdb\u5236\u6a21\u5f0f\u7684\u8bdd\uff0c\u6253\u5370\u5c31\u4f1a\u51fa\u9519\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.3 \u4f7f\u7528\u5176\u4ed6\u5206\u9694\u7b26\u6216\u884c\u7ec8\u6b62\u7b26\u6253\u5370\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u4f7f\u7528 print() \u51fd\u6570\u8f93\u51fa\u6570\u636e\uff0c\u4f46\u662f\u60f3\u6539\u53d8\u9ed8\u8ba4\u7684\u5206\u9694\u7b26\u6216\u8005\u884c\u5c3e\u7b26\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53ef\u4ee5\u4f7f\u7528\u5728 print() \u51fd\u6570\u4e2d\u4f7f\u7528 sep \u548c end \u5173\u952e\u5b57\u53c2\u6570\uff0c\u4ee5\u4f60\u60f3\u8981\u7684\u65b9\u5f0f\u8f93\u51fa\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print('ACME', 50, 91.5)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print('ACME', 50, 91.5, sep=',')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print('ACME', 50, 91.5, sep=',', end='!!\\n')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528 end \u53c2\u6570\u4e5f\u53ef\u4ee5\u5728\u8f93\u51fa\u4e2d\u7981\u6b62\u6362\u884c\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for i in range(5):\n    print(i)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for i in range(5):\n    print(i, end=' ')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u4f60\u60f3\u4f7f\u7528\u975e\u7a7a\u683c\u5206\u9694\u7b26\u6765\u8f93\u51fa\u6570\u636e\u7684\u65f6\u5019\uff0c\u7ed9 print() \u51fd\u6570\u4f20\u9012\u4e00\u4e2a sep \u53c2\u6570\u662f\u6700\u7b80\u5355\u7684\u65b9\u6848\u3002\n\u6709\u65f6\u5019\u4f60\u4f1a\u770b\u5230\u4e00\u4e9b\u7a0b\u5e8f\u5458\u4f1a\u4f7f\u7528 str.join() \u6765\u5b8c\u6210\u540c\u6837\u7684\u4e8b\u60c5\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print(','.join(('ACME','50','91.5')))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "str.join() \u7684\u95ee\u9898\u5728\u4e8e\u5b83\u4ec5\u4ec5\u9002\u7528\u4e8e\u5b57\u7b26\u4e32\u3002\u8fd9\u610f\u5473\u7740\u4f60\u901a\u5e38\u9700\u8981\u6267\u884c\u53e6\u5916\u4e00\u4e9b\u8f6c\u6362\u624d\u80fd\u8ba9\u5b83\u6b63\u5e38\u5de5\u4f5c\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "row = ('ACME', 50, 91.5)\nprint(','.join(row))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print(','.join(str(x) for x in row))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u5f53\u7136\u53ef\u4ee5\u4e0d\u7528\u90a3\u4e48\u9ebb\u70e6\uff0c\u53ea\u9700\u8981\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print(*row, sep=',')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.4 \u8bfb\u5199\u5b57\u8282\u6570\u636e\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u8bfb\u5199\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u6bd4\u5982\u56fe\u7247\uff0c\u58f0\u97f3\u6587\u4ef6\u7b49\u7b49\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u6a21\u5f0f\u4e3a rb \u6216 wb \u7684 open() \u51fd\u6570\u6765\u8bfb\u53d6\u6216\u5199\u5165\u4e8c\u8fdb\u5236\u6570\u636e\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Read the entire file as a single byte string\nwith open('somefile.bin', 'rb') as f:\n    data = f.read()\n\n# Write binary data to a file\nwith open('somefile.bin', 'wb') as f:\n    f.write(b'Hello World')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6570\u636e\u65f6\uff0c\u9700\u8981\u6307\u660e\u7684\u662f\u6240\u6709\u8fd4\u56de\u7684\u6570\u636e\u90fd\u662f\u5b57\u8282\u5b57\u7b26\u4e32\u683c\u5f0f\u7684\uff0c\u800c\u4e0d\u662f\u6587\u672c\u5b57\u7b26\u4e32\u3002\n\u7c7b\u4f3c\u7684\uff0c\u5728\u5199\u5165\u7684\u65f6\u5019\uff0c\u5fc5\u987b\u4fdd\u8bc1\u53c2\u6570\u662f\u4ee5\u5b57\u8282\u5f62\u5f0f\u5bf9\u5916\u66b4\u9732\u6570\u636e\u7684\u5bf9\u8c61(\u6bd4\u5982\u5b57\u8282\u5b57\u7b26\u4e32\uff0c\u5b57\u8282\u6570\u7ec4\u5bf9\u8c61\u7b49)\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u65f6\u5019\uff0c\u5b57\u8282\u5b57\u7b26\u4e32\u548c\u6587\u672c\u5b57\u7b26\u4e32\u7684\u8bed\u4e49\u5dee\u5f02\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e2a\u6f5c\u5728\u7684\u9677\u9631\u3002\n\u7279\u522b\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7d22\u5f15\u548c\u8fed\u4ee3\u52a8\u4f5c\u8fd4\u56de\u7684\u662f\u5b57\u8282\u7684\u503c\u800c\u4e0d\u662f\u5b57\u8282\u5b57\u7b26\u4e32\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Text string\nt = 'Hello World'\nt[0]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for c in t:\n    print(c)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Byte string\nb = b'Hello World'\nb[0]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for c in b:\n    print(c)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u4ece\u4e8c\u8fdb\u5236\u6a21\u5f0f\u7684\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6216\u5199\u5165\u6587\u672c\u6570\u636e\uff0c\u5fc5\u987b\u786e\u4fdd\u8981\u8fdb\u884c\u89e3\u7801\u548c\u7f16\u7801\u64cd\u4f5c\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with open('somefile.bin', 'rb') as f:\n    data = f.read(16)\n    text = data.decode('utf-8')\n\nwith open('somefile.bin', 'wb') as f:\n    text = 'Hello World'\n    f.write(text.encode('utf-8'))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e8c\u8fdb\u5236I/O\u8fd8\u6709\u4e00\u4e2a\u9c9c\u4e3a\u4eba\u77e5\u7684\u7279\u6027\u5c31\u662f\u6570\u7ec4\u548cC\u7ed3\u6784\u4f53\u7c7b\u578b\u80fd\u76f4\u63a5\u88ab\u5199\u5165\uff0c\u800c\u4e0d\u9700\u8981\u4e2d\u95f4\u8f6c\u6362\u4e3a\u81ea\u5df1\u5bf9\u8c61\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import array\nnums = array.array('i', [1, 2, 3, 4])\nwith open('data.bin','wb') as f:\n    f.write(nums)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u9002\u7528\u4e8e\u4efb\u4f55\u5b9e\u73b0\u4e86\u88ab\u79f0\u4e4b\u4e3a\u201d\u7f13\u51b2\u63a5\u53e3\u201d\u7684\u5bf9\u8c61\uff0c\u8fd9\u79cd\u5bf9\u8c61\u4f1a\u76f4\u63a5\u66b4\u9732\u5176\u5e95\u5c42\u7684\u5185\u5b58\u7f13\u51b2\u533a\u7ed9\u80fd\u5904\u7406\u5b83\u7684\u64cd\u4f5c\u3002\n\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u5199\u5165\u5c31\u662f\u8fd9\u7c7b\u64cd\u4f5c\u4e4b\u4e00\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f88\u591a\u5bf9\u8c61\u8fd8\u5141\u8bb8\u901a\u8fc7\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7684 readinto() \u65b9\u6cd5\u76f4\u63a5\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6570\u636e\u5230\u5176\u5e95\u5c42\u7684\u5185\u5b58\u4e2d\u53bb\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import array\na = array.array('i', [0, 0, 0, 0, 0, 0, 0, 0])\nwith open('data.bin', 'rb') as f:\n    f.readinto(a)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f46\u662f\u4f7f\u7528\u8fd9\u79cd\u6280\u672f\u7684\u65f6\u5019\u9700\u8981\u683c\u5916\u5c0f\u5fc3\uff0c\u56e0\u4e3a\u5b83\u901a\u5e38\u5177\u6709\u5e73\u53f0\u76f8\u5173\u6027\uff0c\u5e76\u4e14\u53ef\u80fd\u4f1a\u4f9d\u8d56\u5b57\u957f\u548c\u5b57\u8282\u987a\u5e8f(\u9ad8\u4f4d\u4f18\u5148\u548c\u4f4e\u4f4d\u4f18\u5148)\u3002\n\u53ef\u4ee5\u67e5\u770b5.9\u5c0f\u8282\u4e2d\u53e6\u5916\u4e00\u4e2a\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6570\u636e\u5230\u53ef\u4fee\u6539\u7f13\u51b2\u533a\u7684\u4f8b\u5b50\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.5 \u6587\u4ef6\u4e0d\u5b58\u5728\u624d\u80fd\u5199\u5165\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u50cf\u4e00\u4e2a\u6587\u4ef6\u4e2d\u5199\u5165\u6570\u636e\uff0c\u4f46\u662f\u524d\u63d0\u5fc5\u987b\u662f\u8fd9\u4e2a\u6587\u4ef6\u5728\u6587\u4ef6\u7cfb\u7edf\u4e0a\u4e0d\u5b58\u5728\u3002\n\u4e5f\u5c31\u662f\u4e0d\u5141\u8bb8\u8986\u76d6\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u5185\u5bb9\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53ef\u4ee5\u5728 open() \u51fd\u6570\u4e2d\u4f7f\u7528 x \u6a21\u5f0f\u6765\u4ee3\u66ff w \u6a21\u5f0f\u7684\u65b9\u6cd5\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with open('somefile', 'wt') as f:\n    f.write('Hello\\n')\nwith open('somefile', 'xt') as f:\n    f.write('Hello\\n')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u6587\u4ef6\u662f\u4e8c\u8fdb\u5236\u7684\uff0c\u4f7f\u7528 xb \u6765\u4ee3\u66ff xt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e00\u5c0f\u8282\u6f14\u793a\u4e86\u5728\u5199\u6587\u4ef6\u65f6\u901a\u5e38\u4f1a\u9047\u5230\u7684\u4e00\u4e2a\u95ee\u9898\u7684\u5b8c\u7f8e\u89e3\u51b3\u65b9\u6848(\u4e0d\u5c0f\u5fc3\u8986\u76d6\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u6587\u4ef6)\u3002\n\u4e00\u4e2a\u66ff\u4ee3\u65b9\u6848\u662f\u5148\u6d4b\u8bd5\u8fd9\u4e2a\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import os\nif not os.path.exists('somefile'):\n    with open('somefile', 'wt') as f:\n        f.write('Hello\\n')\nelse:\n    print('File already exists!')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u663e\u800c\u6613\u89c1\uff0c\u4f7f\u7528x\u6587\u4ef6\u6a21\u5f0f\u66f4\u52a0\u7b80\u5355\u3002\u8981\u6ce8\u610f\u7684\u662fx\u6a21\u5f0f\u662f\u4e00\u4e2aPython3\u5bf9 open() \u51fd\u6570\u7279\u6709\u7684\u6269\u5c55\u3002\n\u5728Python\u7684\u65e7\u7248\u672c\u6216\u8005\u662fPython\u5b9e\u73b0\u7684\u5e95\u5c42C\u51fd\u6570\u5e93\u4e2d\u90fd\u662f\u6ca1\u6709\u8fd9\u4e2a\u6a21\u5f0f\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.6 \u5b57\u7b26\u4e32\u7684I/O\u64cd\u4f5c\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u4f7f\u7528\u64cd\u4f5c\u7c7b\u6587\u4ef6\u5bf9\u8c61\u7684\u7a0b\u5e8f\u6765\u64cd\u4f5c\u6587\u672c\u6216\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528 io.StringIO() \u548c io.BytesIO() \u7c7b\u6765\u521b\u5efa\u7c7b\u6587\u4ef6\u5bf9\u8c61\u64cd\u4f5c\u5b57\u7b26\u4e32\u6570\u636e\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = io.StringIO()\ns.write('Hello World\\n')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "print('This is a test', file=s)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Get all of the data written so far\ns.getvalue()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Wrap a file interface around an existing string\ns = io.StringIO('Hello\\nWorld\\n')\ns.read(4)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s.read()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "io.StringIO \u53ea\u80fd\u7528\u4e8e\u6587\u672c\u3002\u5982\u679c\u4f60\u8981\u64cd\u4f5c\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u8981\u4f7f\u7528 io.BytesIO \u7c7b\u6765\u4ee3\u66ff\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = io.BytesIO()\ns.write(b'binary data')\ns.getvalue()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u4f60\u60f3\u6a21\u62df\u4e00\u4e2a\u666e\u901a\u7684\u6587\u4ef6\u7684\u65f6\u5019 StringIO \u548c BytesIO \u7c7b\u662f\u5f88\u6709\u7528\u7684\u3002\n\u6bd4\u5982\uff0c\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 StringIO \u6765\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6d4b\u8bd5\u6570\u636e\u7684\u7c7b\u6587\u4ef6\u5bf9\u8c61\uff0c\n\u8fd9\u4e2a\u5bf9\u8c61\u53ef\u4ee5\u88ab\u4f20\u7ed9\u67d0\u4e2a\u53c2\u6570\u4e3a\u666e\u901a\u6587\u4ef6\u5bf9\u8c61\u7684\u51fd\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c StringIO \u548c BytesIO \u5b9e\u4f8b\u5e76\u6ca1\u6709\u6b63\u786e\u7684\u6574\u6570\u7c7b\u578b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002\n\u56e0\u6b64\uff0c\u5b83\u4eec\u4e0d\u80fd\u5728\u90a3\u4e9b\u9700\u8981\u4f7f\u7528\u771f\u5b9e\u7684\u7cfb\u7edf\u7ea7\u6587\u4ef6\u5982\u6587\u4ef6\uff0c\u7ba1\u9053\u6216\u8005\u662f\u5957\u63a5\u5b57\u7684\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.7 \u8bfb\u5199\u538b\u7f29\u6587\u4ef6\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u8bfb\u5199\u4e00\u4e2agzip\u6216bz2\u683c\u5f0f\u7684\u538b\u7f29\u6587\u4ef6\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "gzip \u548c bz2 \u6a21\u5757\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u5904\u7406\u8fd9\u4e9b\u6587\u4ef6\u3002\n\u4e24\u4e2a\u6a21\u5757\u90fd\u4e3a open() \u51fd\u6570\u63d0\u4f9b\u4e86\u53e6\u5916\u7684\u5b9e\u73b0\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\n\u6bd4\u5982\uff0c\u4e3a\u4e86\u4ee5\u6587\u672c\u5f62\u5f0f\u8bfb\u53d6\u538b\u7f29\u6587\u4ef6\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# gzip compression\nimport gzip\nwith gzip.open('somefile.gz', 'rt') as f:\n    text = f.read()\n\n# bz2 compression\nimport bz2\nwith bz2.open('somefile.bz2', 'rt') as f:\n    text = f.read()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7c7b\u4f3c\u7684\uff0c\u4e3a\u4e86\u5199\u5165\u538b\u7f29\u6570\u636e\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# gzip compression\nimport gzip\nwith gzip.open('somefile.gz', 'wt') as f:\n    f.write(text)\n\n# bz2 compression\nimport bz2\nwith bz2.open('somefile.bz2', 'wt') as f:\n    f.write(text)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u4e0a\uff0c\u6240\u6709\u7684I/O\u64cd\u4f5c\u90fd\u4f7f\u7528\u6587\u672c\u6a21\u5f0f\u5e76\u6267\u884cUnicode\u7684\u7f16\u7801/\u89e3\u7801\u3002\n\u7c7b\u4f3c\u7684\uff0c\u5982\u679c\u4f60\u60f3\u64cd\u4f5c\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u4f7f\u7528 rb \u6216\u8005 wb \u6587\u4ef6\u6a21\u5f0f\u5373\u53ef\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\u8bfb\u5199\u538b\u7f29\u6570\u636e\u90fd\u662f\u5f88\u7b80\u5355\u7684\u3002\u4f46\u662f\u8981\u6ce8\u610f\u7684\u662f\u9009\u62e9\u4e00\u4e2a\u6b63\u786e\u7684\u6587\u4ef6\u6a21\u5f0f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\n\u5982\u679c\u4f60\u4e0d\u6307\u5b9a\u6a21\u5f0f\uff0c\u90a3\u4e48\u9ed8\u8ba4\u7684\u5c31\u662f\u4e8c\u8fdb\u5236\u6a21\u5f0f\uff0c\u5982\u679c\u8fd9\u65f6\u5019\u7a0b\u5e8f\u60f3\u8981\u63a5\u53d7\u7684\u662f\u6587\u672c\u6570\u636e\uff0c\u90a3\u4e48\u5c31\u4f1a\u51fa\u9519\u3002\ngzip.open() \u548c bz2.open() \u63a5\u53d7\u8ddf\u5185\u7f6e\u7684 open() \u51fd\u6570\u4e00\u6837\u7684\u53c2\u6570\uff0c\n\u5305\u62ec encoding\uff0cerrors\uff0cnewline \u7b49\u7b49\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u5199\u5165\u538b\u7f29\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 compresslevel \u8fd9\u4e2a\u53ef\u9009\u7684\u5173\u952e\u5b57\u53c2\u6570\u6765\u6307\u5b9a\u4e00\u4e2a\u538b\u7f29\u7ea7\u522b\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with gzip.open('somefile.gz', 'wt', compresslevel=5) as f:\n    f.write(text)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9ed8\u8ba4\u7684\u7b49\u7ea7\u662f9\uff0c\u4e5f\u662f\u6700\u9ad8\u7684\u538b\u7f29\u7b49\u7ea7\u3002\u7b49\u7ea7\u8d8a\u4f4e\u6027\u80fd\u8d8a\u597d\uff0c\u4f46\u662f\u6570\u636e\u538b\u7f29\u7a0b\u5ea6\u4e5f\u8d8a\u4f4e\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u4e00\u70b9\uff0c gzip.open() \u548c bz2.open() \u8fd8\u6709\u4e00\u4e2a\u5f88\u5c11\u88ab\u77e5\u9053\u7684\u7279\u6027\uff0c\n\u5b83\u4eec\u53ef\u4ee5\u4f5c\u7528\u5728\u4e00\u4e2a\u5df2\u5b58\u5728\u5e76\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u7684\u6587\u4ef6\u4e0a\u3002\u6bd4\u5982\uff0c\u4e0b\u9762\u4ee3\u7801\u662f\u53ef\u884c\u7684\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import gzip\nf = open('somefile.gz', 'rb')\nwith gzip.open(f, 'rt') as g:\n    text = g.read()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6837\u5c31\u5141\u8bb8 gzip \u548c bz2 \u6a21\u5757\u53ef\u4ee5\u5de5\u4f5c\u5728\u8bb8\u591a\u7c7b\u6587\u4ef6\u5bf9\u8c61\u4e0a\uff0c\u6bd4\u5982\u5957\u63a5\u5b57\uff0c\u7ba1\u9053\u548c\u5185\u5b58\u4e2d\u6587\u4ef6\u7b49\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.8 \u56fa\u5b9a\u5927\u5c0f\u8bb0\u5f55\u7684\u6587\u4ef6\u8fed\u4ee3\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5728\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u8bb0\u5f55\u6216\u8005\u6570\u636e\u5757\u7684\u96c6\u5408\u4e0a\u8fed\u4ee3\uff0c\u800c\u4e0d\u662f\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u4e00\u884c\u4e00\u884c\u7684\u8fed\u4ee3\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u8fc7\u4e0b\u9762\u8fd9\u4e2a\u5c0f\u6280\u5de7\u4f7f\u7528 iter \u548c functools.partial() \u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from functools import partial\n\nRECORD_SIZE = 32\n\nwith open('somefile.data', 'rb') as f:\n    records = iter(partial(f.read, RECORD_SIZE), b'')\n    for r in records:\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u7684 records \u5bf9\u8c61\u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5b83\u4f1a\u4e0d\u65ad\u7684\u4ea7\u751f\u56fa\u5b9a\u5927\u5c0f\u7684\u6570\u636e\u5757\uff0c\u76f4\u5230\u6587\u4ef6\u672b\u5c3e\u3002\n\u8981\u6ce8\u610f\u7684\u662f\u5982\u679c\u603b\u8bb0\u5f55\u5927\u5c0f\u4e0d\u662f\u5757\u5927\u5c0f\u7684\u6574\u6570\u500d\u7684\u8bdd\uff0c\u6700\u540e\u4e00\u4e2a\u8fd4\u56de\u5143\u7d20\u7684\u5b57\u8282\u6570\u4f1a\u6bd4\u671f\u671b\u503c\u5c11\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "iter() \u51fd\u6570\u6709\u4e00\u4e2a\u9c9c\u4e3a\u4eba\u77e5\u7684\u7279\u6027\u5c31\u662f\uff0c\u5982\u679c\u4f60\u7ed9\u5b83\u4f20\u9012\u4e00\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\u548c\u4e00\u4e2a\u6807\u8bb0\u503c\uff0c\u5b83\u4f1a\u521b\u5efa\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\n\u8fd9\u4e2a\u8fed\u4ee3\u5668\u4f1a\u4e00\u76f4\u8c03\u7528\u4f20\u5165\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u76f4\u5230\u5b83\u8fd4\u56de\u6807\u8bb0\u503c\u4e3a\u6b62\uff0c\u8fd9\u65f6\u5019\u8fed\u4ee3\u7ec8\u6b62\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u4f8b\u5b50\u4e2d\uff0c functools.partial \u7528\u6765\u521b\u5efa\u4e00\u4e2a\u6bcf\u6b21\u88ab\u8c03\u7528\u65f6\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u56fa\u5b9a\u6570\u76ee\u5b57\u8282\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u3002\n\u6807\u8bb0\u503c b'' \u5c31\u662f\u5f53\u5230\u8fbe\u6587\u4ef6\u7ed3\u5c3e\u65f6\u7684\u8fd4\u56de\u503c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u518d\u63d0\u4e00\u70b9\uff0c\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\u7684\u6587\u4ef6\u65f6\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u7684\u3002\n\u5982\u679c\u662f\u8bfb\u53d6\u56fa\u5b9a\u5927\u5c0f\u7684\u8bb0\u5f55\uff0c\u8fd9\u901a\u5e38\u662f\u6700\u666e\u904d\u7684\u60c5\u51b5\u3002\n\u800c\u5bf9\u4e8e\u6587\u672c\u6587\u4ef6\uff0c\u4e00\u884c\u4e00\u884c\u7684\u8bfb\u53d6(\u9ed8\u8ba4\u7684\u8fed\u4ee3\u884c\u4e3a)\u66f4\u666e\u904d\u70b9\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.9 \u8bfb\u53d6\u4e8c\u8fdb\u5236\u6570\u636e\u5230\u53ef\u53d8\u7f13\u51b2\u533a\u4e2d\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u76f4\u63a5\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6570\u636e\u5230\u4e00\u4e2a\u53ef\u53d8\u7f13\u51b2\u533a\u4e2d\uff0c\u800c\u4e0d\u9700\u8981\u505a\u4efb\u4f55\u7684\u4e2d\u95f4\u590d\u5236\u64cd\u4f5c\u3002\n\u6216\u8005\u4f60\u60f3\u539f\u5730\u4fee\u6539\u6570\u636e\u5e76\u5c06\u5b83\u5199\u56de\u5230\u4e00\u4e2a\u6587\u4ef6\u4e2d\u53bb\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u8bfb\u53d6\u6570\u636e\u5230\u4e00\u4e2a\u53ef\u53d8\u6570\u7ec4\u4e2d\uff0c\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7684 readinto() \u65b9\u6cd5\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import os.path\n\ndef read_into_buffer(filename):\n    buf = bytearray(os.path.getsize(filename))\n    with open(filename, 'rb') as f:\n        f.readinto(buf)\n    return buf"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4e00\u4e2a\u6f14\u793a\u8fd9\u4e2a\u51fd\u6570\u4f7f\u7528\u65b9\u6cd5\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Write a sample file\nwith open('sample.bin', 'wb') as f:\n    f.write(b'Hello World')\nbuf = read_into_buffer('sample.bin')\nbuf"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "buf[0:5] = b'Hello'\nbuf"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with open('newsample.bin', 'wb') as f:\n    f.write(buf)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6587\u4ef6\u5bf9\u8c61\u7684 readinto() \u65b9\u6cd5\u80fd\u88ab\u7528\u6765\u4e3a\u9884\u5148\u5206\u914d\u5185\u5b58\u7684\u6570\u7ec4\u586b\u5145\u6570\u636e\uff0c\u751a\u81f3\u5305\u62ec\u7531 array \u6a21\u5757\u6216 numpy \u5e93\u521b\u5efa\u7684\u6570\u7ec4\u3002\n\u548c\u666e\u901a read() \u65b9\u6cd5\u4e0d\u540c\u7684\u662f\uff0c readinto() \u586b\u5145\u5df2\u5b58\u5728\u7684\u7f13\u51b2\u533a\u800c\u4e0d\u662f\u4e3a\u65b0\u5bf9\u8c61\u91cd\u65b0\u5206\u914d\u5185\u5b58\u518d\u8fd4\u56de\u5b83\u4eec\u3002\n\u56e0\u6b64\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u907f\u514d\u5927\u91cf\u7684\u5185\u5b58\u5206\u914d\u64cd\u4f5c\u3002\n\u6bd4\u5982\uff0c\u5982\u679c\u4f60\u8bfb\u53d6\u4e00\u4e2a\u7531\u76f8\u540c\u5927\u5c0f\u7684\u8bb0\u5f55\u7ec4\u6210\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u65f6\uff0c\u4f60\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "record_size = 32 # Size of each record (adjust value)\n\nbuf = bytearray(record_size)\nwith open('somefile', 'rb') as f:\n    while True:\n        n = f.readinto(buf)\n        if n < record_size:\n            break\n        # Use the contents of buf\n        ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53e6\u5916\u6709\u4e00\u4e2a\u6709\u8da3\u7279\u6027\u5c31\u662f memoryview \uff0c\n\u5b83\u53ef\u4ee5\u901a\u8fc7\u96f6\u590d\u5236\u7684\u65b9\u5f0f\u5bf9\u5df2\u5b58\u5728\u7684\u7f13\u51b2\u533a\u6267\u884c\u5207\u7247\u64cd\u4f5c\uff0c\u751a\u81f3\u8fd8\u80fd\u4fee\u6539\u5b83\u7684\u5185\u5bb9\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "buf"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m1 = memoryview(buf)\nm2 = m1[-5:]\nm2"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m2[:] = b'WORLD'\nbuf"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528 f.readinto() \u65f6\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f60\u5fc5\u987b\u68c0\u67e5\u5b83\u7684\u8fd4\u56de\u503c\uff0c\u4e5f\u5c31\u662f\u5b9e\u9645\u8bfb\u53d6\u7684\u5b57\u8282\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u5b57\u8282\u6570\u5c0f\u4e8e\u7f13\u51b2\u533a\u5927\u5c0f\uff0c\u8868\u660e\u6570\u636e\u88ab\u622a\u65ad\u6216\u8005\u88ab\u7834\u574f\u4e86(\u6bd4\u5982\u4f60\u671f\u671b\u6bcf\u6b21\u8bfb\u53d6\u6307\u5b9a\u6570\u91cf\u7684\u5b57\u8282)\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u7559\u5fc3\u89c2\u5bdf\u5176\u4ed6\u51fd\u6570\u5e93\u548c\u6a21\u5757\u4e2d\u548c into \u76f8\u5173\u7684\u51fd\u6570(\u6bd4\u5982 recv_into() \uff0c pack_into() \u7b49)\u3002\nPython\u7684\u5f88\u591a\u5176\u4ed6\u90e8\u5206\u5df2\u7ecf\u80fd\u652f\u6301\u76f4\u63a5\u7684I/O\u6216\u6570\u636e\u8bbf\u95ee\u64cd\u4f5c\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u53ef\u88ab\u7528\u6765\u586b\u5145\u6216\u4fee\u6539\u6570\u7ec4\u548c\u7f13\u51b2\u533a\u5185\u5bb9\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5173\u4e8e\u89e3\u6790\u4e8c\u8fdb\u5236\u7ed3\u6784\u548c memoryviews \u4f7f\u7528\u65b9\u6cd5\u7684\u66f4\u9ad8\u7ea7\u4f8b\u5b50\uff0c\u8bf7\u53c2\u80036.12\u5c0f\u8282\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.10 \u5185\u5b58\u6620\u5c04\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5185\u5b58\u6620\u5c04\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\u5230\u4e00\u4e2a\u53ef\u53d8\u5b57\u8282\u6570\u7ec4\u4e2d\uff0c\u76ee\u7684\u53ef\u80fd\u662f\u4e3a\u4e86\u968f\u673a\u8bbf\u95ee\u5b83\u7684\u5185\u5bb9\u6216\u8005\u662f\u539f\u5730\u505a\u4e9b\u4fee\u6539\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528 mmap \u6a21\u5757\u6765\u5185\u5b58\u6620\u5c04\u6587\u4ef6\u3002\n\u4e0b\u9762\u662f\u4e00\u4e2a\u5de5\u5177\u51fd\u6570\uff0c\u5411\u4f60\u6f14\u793a\u4e86\u5982\u4f55\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u5e76\u4ee5\u4e00\u79cd\u4fbf\u6377\u65b9\u5f0f\u5185\u5b58\u6620\u5c04\u8fd9\u4e2a\u6587\u4ef6\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import os\nimport mmap\n\ndef memory_map(filename, access=mmap.ACCESS_WRITE):\n    size = os.path.getsize(filename)\n    fd = os.open(filename, os.O_RDWR)\n    return mmap.mmap(fd, size, access=access)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u4f60\u9700\u8981\u6709\u4e00\u4e2a\u5df2\u521b\u5efa\u5e76\u4e14\u5185\u5bb9\u4e0d\u4e3a\u7a7a\u7684\u6587\u4ef6\u3002\n\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u6559\u4f60\u600e\u6837\u521d\u59cb\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5e76\u5c06\u5176\u5185\u5bb9\u6269\u5145\u5230\u6307\u5b9a\u5927\u5c0f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "size = 1000000\nwith open('data', 'wb') as f:\n    f.seek(size-1)\n    f.write(b'\\x00')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4e00\u4e2a\u5229\u7528 memory_map() \u51fd\u6570\u7c7b\u5185\u5b58\u6620\u5c04\u6587\u4ef6\u5185\u5bb9\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m = memory_map('data')\nlen(m)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m[0:10]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m[0]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Reassign a slice\nm[0:11] = b'Hello World'\nm.close()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Verify that changes were made\nwith open('data', 'rb') as f:\nprint(f.read(11))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "mmap() \u8fd4\u56de\u7684 mmap \u5bf9\u8c61\u540c\u6837\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u4f7f\u7528\uff0c\n\u8fd9\u65f6\u5019\u5e95\u5c42\u7684\u6587\u4ef6\u4f1a\u88ab\u81ea\u52a8\u5173\u95ed\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with memory_map('data') as m:\n    print(len(m))\n    print(m[0:10])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m.closed"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c memeory_map() \u51fd\u6570\u6253\u5f00\u7684\u6587\u4ef6\u540c\u65f6\u652f\u6301\u8bfb\u548c\u5199\u64cd\u4f5c\u3002\n\u4efb\u4f55\u7684\u4fee\u6539\u5185\u5bb9\u90fd\u4f1a\u590d\u5236\u56de\u539f\u6765\u7684\u6587\u4ef6\u4e2d\u3002\n\u5982\u679c\u9700\u8981\u53ea\u8bfb\u7684\u8bbf\u95ee\u6a21\u5f0f\uff0c\u53ef\u4ee5\u7ed9\u53c2\u6570 access \u8d4b\u503c\u4e3a mmap.ACCESS_READ \u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m = memory_map(filename, mmap.ACCESS_READ)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u5728\u672c\u5730\u4fee\u6539\u6570\u636e\uff0c\u4f46\u662f\u53c8\u4e0d\u60f3\u5c06\u4fee\u6539\u5199\u56de\u5230\u539f\u59cb\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 mmap.ACCESS_COPY \uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m = memory_map(filename, mmap.ACCESS_COPY)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u968f\u673a\u8bbf\u95ee\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u4f7f\u7528 mmap \u5c06\u6587\u4ef6\u6620\u5c04\u5230\u5185\u5b58\u4e2d\u662f\u4e00\u4e2a\u9ad8\u6548\u548c\u4f18\u96c5\u7684\u65b9\u6cd5\u3002\n\u4f8b\u5982\uff0c\u4f60\u65e0\u9700\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u5e76\u6267\u884c\u5927\u91cf\u7684 seek() \uff0c read() \uff0c write() \u8c03\u7528\uff0c\n\u53ea\u9700\u8981\u7b80\u5355\u7684\u6620\u5c04\u6587\u4ef6\u5e76\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u8bbf\u95ee\u6570\u636e\u5373\u53ef\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u822c\u6765\u8bb2\uff0c mmap() \u6240\u66b4\u9732\u7684\u5185\u5b58\u770b\u4e0a\u53bb\u5c31\u662f\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6570\u7ec4\u5bf9\u8c61\u3002\n\u4f46\u662f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5185\u5b58\u89c6\u56fe\u6765\u89e3\u6790\u5176\u4e2d\u7684\u6570\u636e\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m = memory_map('data')\n# Memoryview of unsigned integers\nv = memoryview(m).cast('I')\nv[0] = 7\nm[0:4]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m[0:4] = b'\\x07\\x01\\x00\\x00'\nv[0]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9700\u8981\u5f3a\u8c03\u7684\u4e00\u70b9\u662f\uff0c\u5185\u5b58\u6620\u5c04\u4e00\u4e2a\u6587\u4ef6\u5e76\u4e0d\u4f1a\u5bfc\u81f4\u6574\u4e2a\u6587\u4ef6\u88ab\u8bfb\u53d6\u5230\u5185\u5b58\u4e2d\u3002\n\u4e5f\u5c31\u662f\u8bf4\uff0c\u6587\u4ef6\u5e76\u6ca1\u6709\u88ab\u590d\u5236\u5230\u5185\u5b58\u7f13\u5b58\u6216\u6570\u7ec4\u4e2d\u3002\u76f8\u53cd\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4ec5\u4ec5\u4e3a\u6587\u4ef6\u5185\u5bb9\u4fdd\u7559\u4e86\u4e00\u6bb5\u865a\u62df\u5185\u5b58\u3002\n\u5f53\u4f60\u8bbf\u95ee\u6587\u4ef6\u7684\u4e0d\u540c\u533a\u57df\u65f6\uff0c\u8fd9\u4e9b\u533a\u57df\u7684\u5185\u5bb9\u624d\u6839\u636e\u9700\u8981\u88ab\u8bfb\u53d6\u5e76\u6620\u5c04\u5230\u5185\u5b58\u533a\u57df\u4e2d\u3002\n\u800c\u90a3\u4e9b\u4ece\u6ca1\u88ab\u8bbf\u95ee\u5230\u7684\u90e8\u5206\u8fd8\u662f\u7559\u5728\u78c1\u76d8\u4e0a\u3002\u6240\u6709\u8fd9\u4e9b\u8fc7\u7a0b\u662f\u900f\u660e\u7684\uff0c\u5728\u5e55\u540e\u5b8c\u6210\uff01"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u591a\u4e2aPython\u89e3\u91ca\u5668\u5185\u5b58\u6620\u5c04\u540c\u4e00\u4e2a\u6587\u4ef6\uff0c\u5f97\u5230\u7684 mmap \u5bf9\u8c61\u80fd\u591f\u88ab\u7528\u6765\u5728\u89e3\u91ca\u5668\u76f4\u63a5\u4ea4\u6362\u6570\u636e\u3002\n\u4e5f\u5c31\u662f\u8bf4\uff0c\u6240\u6709\u89e3\u91ca\u5668\u90fd\u80fd\u540c\u65f6\u8bfb\u5199\u6570\u636e\uff0c\u5e76\u4e14\u5176\u4e2d\u4e00\u4e2a\u89e3\u91ca\u5668\u6240\u505a\u7684\u4fee\u6539\u4f1a\u81ea\u52a8\u5448\u73b0\u5728\u5176\u4ed6\u89e3\u91ca\u5668\u4e2d\u3002\n\u5f88\u660e\u663e\uff0c\u8fd9\u91cc\u9700\u8981\u8003\u8651\u540c\u6b65\u7684\u95ee\u9898\u3002\u4f46\u662f\u8fd9\u79cd\u65b9\u6cd5\u6709\u65f6\u5019\u53ef\u4ee5\u7528\u6765\u5728\u7ba1\u9053\u6216\u5957\u63a5\u5b57\u95f4\u4f20\u9012\u6570\u636e\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e00\u5c0f\u8282\u4e2d\u51fd\u6570\u5c3d\u91cf\u5199\u5f97\u5f88\u901a\u7528\uff0c\u540c\u65f6\u9002\u7528\u4e8eUnix\u548cWindows\u5e73\u53f0\u3002\n\u8981\u6ce8\u610f\u7684\u662f\u4f7f\u7528 mmap() \u51fd\u6570\u65f6\u4f1a\u5728\u5e95\u5c42\u6709\u4e00\u4e9b\u5e73\u53f0\u7684\u5dee\u5f02\u6027\u3002\n\u53e6\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u9009\u9879\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u533f\u540d\u7684\u5185\u5b58\u6620\u5c04\u533a\u57df\u3002\n\u5982\u679c\u4f60\u5bf9\u8fd9\u4e2a\u611f\u5174\u8da3\uff0c\u786e\u4fdd\u4f60\u4ed4\u7ec6\u7814\u8bfb\u4e86Python\u6587\u6863\u4e2d\n\u8fd9\u65b9\u9762\u7684\u5185\u5bb9 \u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.11 \u6587\u4ef6\u8def\u5f84\u540d\u7684\u64cd\u4f5c\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u9700\u8981\u4f7f\u7528\u8def\u5f84\u540d\u6765\u83b7\u53d6\u6587\u4ef6\u540d\uff0c\u76ee\u5f55\u540d\uff0c\u7edd\u5bf9\u8def\u5f84\u7b49\u7b49\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528 os.path \u6a21\u5757\u4e2d\u7684\u51fd\u6570\u6765\u64cd\u4f5c\u8def\u5f84\u540d\u3002\n\u4e0b\u9762\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u4f8b\u5b50\u6765\u6f14\u793a\u4e00\u4e9b\u5173\u952e\u7684\u7279\u6027\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import os\npath = '/Users/beazley/Data/data.csv'"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Get the last component of the path\nos.path.basename(path)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Get the directory name\nos.path.dirname(path)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Join path components together\nos.path.join('tmp', 'data', os.path.basename(path))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Expand the user's home directory\npath = '~/Data/data.csv'\nos.path.expanduser(path)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Split the file extension\nos.path.splitext(path)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u4efb\u4f55\u7684\u6587\u4ef6\u540d\u7684\u64cd\u4f5c\uff0c\u4f60\u90fd\u5e94\u8be5\u4f7f\u7528 os.path \u6a21\u5757\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u6807\u51c6\u5b57\u7b26\u4e32\u64cd\u4f5c\u6765\u6784\u9020\u81ea\u5df1\u7684\u4ee3\u7801\u3002\n\u7279\u522b\u662f\u4e3a\u4e86\u53ef\u79fb\u690d\u6027\u8003\u8651\u7684\u65f6\u5019\u66f4\u5e94\u5982\u6b64\uff0c\n\u56e0\u4e3a os.path \u6a21\u5757\u77e5\u9053Unix\u548cWindows\u7cfb\u7edf\u4e4b\u95f4\u7684\u5dee\u5f02\u5e76\u4e14\u80fd\u591f\u53ef\u9760\u5730\u5904\u7406\u7c7b\u4f3c Data/data.csv\n\u548c Data\\data.csv \u8fd9\u6837\u7684\u6587\u4ef6\u540d\u3002\n\u5176\u6b21\uff0c\u4f60\u771f\u7684\u4e0d\u5e94\u8be5\u6d6a\u8d39\u65f6\u95f4\u53bb\u91cd\u590d\u9020\u8f6e\u5b50\u3002\u901a\u5e38\u6700\u597d\u662f\u76f4\u63a5\u4f7f\u7528\u5df2\u7ecf\u4e3a\u4f60\u51c6\u5907\u597d\u7684\u529f\u80fd\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u6ce8\u610f\u7684\u662f os.path \u8fd8\u6709\u66f4\u591a\u7684\u529f\u80fd\u5728\u8fd9\u91cc\u5e76\u6ca1\u6709\u5217\u4e3e\u51fa\u6765\u3002\n\u53ef\u4ee5\u67e5\u9605\u5b98\u65b9\u6587\u6863\u6765\u83b7\u53d6\u66f4\u591a\u4e0e\u6587\u4ef6\u6d4b\u8bd5\uff0c\u7b26\u53f7\u94fe\u63a5\u7b49\u76f8\u5173\u7684\u51fd\u6570\u8bf4\u660e\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.12 \u6d4b\u8bd5\u6587\u4ef6\u662f\u5426\u5b58\u5728\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u6d4b\u8bd5\u4e00\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55\u662f\u5426\u5b58\u5728\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528 os.path \u6a21\u5757\u6765\u6d4b\u8bd5\u4e00\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55\u662f\u5426\u5b58\u5728\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import os\nos.path.exists('/etc/passwd')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "os.path.exists('/tmp/spam')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u8fd8\u80fd\u8fdb\u4e00\u6b65\u6d4b\u8bd5\u8fd9\u4e2a\u6587\u4ef6\u65f6\u4ec0\u4e48\u7c7b\u578b\u7684\u3002\n\u5728\u4e0b\u9762\u8fd9\u4e9b\u6d4b\u8bd5\u4e2d\uff0c\u5982\u679c\u6d4b\u8bd5\u7684\u6587\u4ef6\u4e0d\u5b58\u5728\u7684\u65f6\u5019\uff0c\u7ed3\u679c\u90fd\u4f1a\u8fd4\u56deFalse\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Is a regular file\nos.path.isfile('/etc/passwd')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Is a directory\nos.path.isdir('/etc/passwd')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Is a symbolic link\nos.path.islink('/usr/local/bin/python3')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Get the file linked to\nos.path.realpath('/usr/local/bin/python3')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u8fd8\u60f3\u83b7\u53d6\u5143\u6570\u636e(\u6bd4\u5982\u6587\u4ef6\u5927\u5c0f\u6216\u8005\u662f\u4fee\u6539\u65e5\u671f)\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 os.path \u6a21\u5757\u6765\u89e3\u51b3\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "os.path.getsize('/etc/passwd')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "os.path.getmtime('/etc/passwd')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import time\ntime.ctime(os.path.getmtime('/etc/passwd'))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528 os.path \u6765\u8fdb\u884c\u6587\u4ef6\u6d4b\u8bd5\u662f\u5f88\u7b80\u5355\u7684\u3002\n\u5728\u5199\u8fd9\u4e9b\u811a\u672c\u65f6\uff0c\u53ef\u80fd\u552f\u4e00\u9700\u8981\u6ce8\u610f\u7684\u5c31\u662f\u4f60\u9700\u8981\u8003\u8651\u6587\u4ef6\u6743\u9650\u7684\u95ee\u9898\uff0c\u7279\u522b\u662f\u5728\u83b7\u53d6\u5143\u6570\u636e\u65f6\u5019\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "os.path.getsize('/Users/guido/Desktop/foo.txt')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.13 \u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u5217\u8868\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u83b7\u53d6\u6587\u4ef6\u7cfb\u7edf\u4e2d\u67d0\u4e2a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u5217\u8868\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528 os.listdir() \u51fd\u6570\u6765\u83b7\u53d6\u67d0\u4e2a\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u5217\u8868\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import os\nnames = os.listdir('somedir')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7ed3\u679c\u4f1a\u8fd4\u56de\u76ee\u5f55\u4e2d\u6240\u6709\u6587\u4ef6\u5217\u8868\uff0c\u5305\u62ec\u6240\u6709\u6587\u4ef6\uff0c\u5b50\u76ee\u5f55\uff0c\u7b26\u53f7\u94fe\u63a5\u7b49\u7b49\u3002\n\u5982\u679c\u4f60\u9700\u8981\u901a\u8fc7\u67d0\u79cd\u65b9\u5f0f\u8fc7\u6ee4\u6570\u636e\uff0c\u53ef\u4ee5\u8003\u8651\u7ed3\u5408 os.path \u5e93\u4e2d\u7684\u4e00\u4e9b\u51fd\u6570\u6765\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import os.path\n\n# Get all regular files\nnames = [name for name in os.listdir('somedir')\n        if os.path.isfile(os.path.join('somedir', name))]\n\n# Get all dirs\ndirnames = [name for name in os.listdir('somedir')\n        if os.path.isdir(os.path.join('somedir', name))]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b57\u7b26\u4e32\u7684 startswith() \u548c endswith() \u65b9\u6cd5\u5bf9\u4e8e\u8fc7\u6ee4\u4e00\u4e2a\u76ee\u5f55\u7684\u5185\u5bb9\u4e5f\u662f\u5f88\u6709\u7528\u7684\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "pyfiles = [name for name in os.listdir('somedir')\n            if name.endswith('.py')]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u6587\u4ef6\u540d\u7684\u5339\u914d\uff0c\u4f60\u53ef\u80fd\u4f1a\u8003\u8651\u4f7f\u7528 glob \u6216 fnmatch \u6a21\u5757\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import glob\npyfiles = glob.glob('somedir/*.py')\n\nfrom fnmatch import fnmatch\npyfiles = [name for name in os.listdir('somedir')\n            if fnmatch(name, '*.py')]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u83b7\u53d6\u76ee\u5f55\u4e2d\u7684\u5217\u8868\u662f\u5f88\u5bb9\u6613\u7684\uff0c\u4f46\u662f\u5176\u8fd4\u56de\u7ed3\u679c\u53ea\u662f\u76ee\u5f55\u4e2d\u5b9e\u4f53\u540d\u5217\u8868\u800c\u5df2\u3002\n\u5982\u679c\u4f60\u8fd8\u60f3\u83b7\u53d6\u5176\u4ed6\u7684\u5143\u4fe1\u606f\uff0c\u6bd4\u5982\u6587\u4ef6\u5927\u5c0f\uff0c\u4fee\u6539\u65f6\u95f4\u7b49\u7b49\uff0c\n\u4f60\u6216\u8bb8\u8fd8\u9700\u8981\u4f7f\u7528\u5230 os.path \u6a21\u5757\u4e2d\u7684\u51fd\u6570\u6216\u7740 os.stat() \u51fd\u6570\u6765\u6536\u96c6\u6570\u636e\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Example of getting a directory listing\n\nimport os\nimport os.path\nimport glob\n\npyfiles = glob.glob('*.py')\n\n# Get file sizes and modification dates\nname_sz_date = [(name, os.path.getsize(name), os.path.getmtime(name))\n                for name in pyfiles]\nfor name, size, mtime in name_sz_date:\n    print(name, size, mtime)\n\n# Alternative: Get file metadata\nfile_metadata = [(name, os.stat(name)) for name in pyfiles]\nfor name, meta in file_metadata:\n    print(name, meta.st_size, meta.st_mtime)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u8fd8\u6709\u4e00\u70b9\u8981\u6ce8\u610f\u7684\u5c31\u662f\uff0c\u6709\u65f6\u5019\u5728\u5904\u7406\u6587\u4ef6\u540d\u7f16\u7801\u95ee\u9898\u65f6\u5019\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e00\u4e9b\u95ee\u9898\u3002\n\u901a\u5e38\u6765\u8bb2\uff0c\u51fd\u6570 os.listdir() \u8fd4\u56de\u7684\u5b9e\u4f53\u5217\u8868\u4f1a\u6839\u636e\u7cfb\u7edf\u9ed8\u8ba4\u7684\u6587\u4ef6\u540d\u7f16\u7801\u6765\u89e3\u7801\u3002\n\u4f46\u662f\u6709\u65f6\u5019\u4e5f\u4f1a\u78b0\u5230\u4e00\u4e9b\u4e0d\u80fd\u6b63\u5e38\u89e3\u7801\u7684\u6587\u4ef6\u540d\u3002\n\u5173\u4e8e\u6587\u4ef6\u540d\u7684\u5904\u7406\u95ee\u9898\uff0c\u57285.14\u548c5.15\u5c0f\u8282\u6709\u66f4\u8be6\u7ec6\u7684\u8bb2\u89e3\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.14 \u5ffd\u7565\u6587\u4ef6\u540d\u7f16\u7801\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u4f7f\u7528\u539f\u59cb\u6587\u4ef6\u540d\u6267\u884c\u6587\u4ef6\u7684I/O\u64cd\u4f5c\uff0c\u4e5f\u5c31\u662f\u8bf4\u6587\u4ef6\u540d\u5e76\u6ca1\u6709\u7ecf\u8fc7\u7cfb\u7edf\u9ed8\u8ba4\u7f16\u7801\u53bb\u89e3\u7801\u6216\u7f16\u7801\u8fc7\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u7684\u6587\u4ef6\u540d\u90fd\u4f1a\u6839\u636e sys.getfilesystemencoding() \u8fd4\u56de\u7684\u6587\u672c\u7f16\u7801\u6765\u7f16\u7801\u6216\u89e3\u7801\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sys.getfilesystemencoding()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u56e0\u4e3a\u67d0\u79cd\u539f\u56e0\u4f60\u60f3\u5ffd\u7565\u8fd9\u79cd\u7f16\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u539f\u59cb\u5b57\u8282\u5b57\u7b26\u4e32\u6765\u6307\u5b9a\u4e00\u4e2a\u6587\u4ef6\u540d\u5373\u53ef\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Wrte a file using a unicode filename\nwith open('jalape\\xf1o.txt', 'w') as f:\n    f.write('Spicy!')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Directory listing (decoded)\nimport os\nos.listdir('.')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Directory listing (raw)\nos.listdir(b'.') # Note: byte string"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Open file with raw filename\nwith open(b'jalapen\\xcc\\x83o.txt') as f:\n    print(f.read())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6b63\u5982\u4f60\u6240\u89c1\uff0c\u5728\u6700\u540e\u4e24\u4e2a\u64cd\u4f5c\u4e2d\uff0c\u5f53\u4f60\u7ed9\u6587\u4ef6\u76f8\u5173\u51fd\u6570\u5982 open() \u548c os.listdir()\n\u4f20\u9012\u5b57\u8282\u5b57\u7b26\u4e32\u65f6\uff0c\u6587\u4ef6\u540d\u7684\u5904\u7406\u65b9\u5f0f\u4f1a\u7a0d\u6709\u4e0d\u540c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u5e38\u6765\u8bb2\uff0c\u4f60\u4e0d\u9700\u8981\u62c5\u5fc3\u6587\u4ef6\u540d\u7684\u7f16\u7801\u548c\u89e3\u7801\uff0c\u666e\u901a\u7684\u6587\u4ef6\u540d\u64cd\u4f5c\u5e94\u8be5\u5c31\u6ca1\u95ee\u9898\u4e86\u3002\n\u4f46\u662f\uff0c\u6709\u4e9b\u64cd\u4f5c\u7cfb\u7edf\u5141\u8bb8\u7528\u6237\u901a\u8fc7\u5076\u7136\u6216\u6076\u610f\u65b9\u5f0f\u53bb\u521b\u5efa\u540d\u5b57\u4e0d\u7b26\u5408\u9ed8\u8ba4\u7f16\u7801\u7684\u6587\u4ef6\u3002\n\u8fd9\u4e9b\u6587\u4ef6\u540d\u53ef\u80fd\u4f1a\u795e\u79d8\u5730\u4e2d\u65ad\u90a3\u4e9b\u9700\u8981\u5904\u7406\u5927\u91cf\u6587\u4ef6\u7684Python\u7a0b\u5e8f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8bfb\u53d6\u76ee\u5f55\u5e76\u901a\u8fc7\u539f\u59cb\u672a\u89e3\u7801\u65b9\u5f0f\u5904\u7406\u6587\u4ef6\u540d\u53ef\u4ee5\u6709\u6548\u7684\u907f\u514d\u8fd9\u6837\u7684\u95ee\u9898\uff0c\n\u5c3d\u7ba1\u8fd9\u6837\u4f1a\u5e26\u6765\u4e00\u5b9a\u7684\u7f16\u7a0b\u96be\u5ea6\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5173\u4e8e\u6253\u5370\u4e0d\u53ef\u89e3\u7801\u7684\u6587\u4ef6\u540d\uff0c\u8bf7\u53c2\u80035.15\u5c0f\u8282\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.15 \u6253\u5370\u4e0d\u5408\u6cd5\u7684\u6587\u4ef6\u540d\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u7684\u7a0b\u5e8f\u83b7\u53d6\u4e86\u4e00\u4e2a\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u540d\u5217\u8868\uff0c\u4f46\u662f\u5f53\u5b83\u8bd5\u7740\u53bb\u6253\u5370\u6587\u4ef6\u540d\u7684\u65f6\u5019\u7a0b\u5e8f\u5d29\u6e83\uff0c\n\u51fa\u73b0\u4e86 UnicodeEncodeError \u5f02\u5e38\u548c\u4e00\u6761\u5947\u602a\u7684\u6d88\u606f\u2014\u2014 surrogates not allowed \u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u6253\u5370\u672a\u77e5\u7684\u6587\u4ef6\u540d\u65f6\uff0c\u4f7f\u7528\u4e0b\u9762\u7684\u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u8fd9\u6837\u7684\u9519\u8bef\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def bad_filename(filename):\n    return repr(filename)[1:-1]\n\ntry:\n    print(filename)\nexcept UnicodeEncodeError:\n    print(bad_filename(filename))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e00\u5c0f\u8282\u8ba8\u8bba\u7684\u662f\u5728\u7f16\u5199\u5fc5\u987b\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u7684\u7a0b\u5e8f\u65f6\u4e00\u4e2a\u4e0d\u592a\u5e38\u89c1\u4f46\u53c8\u5f88\u68d8\u624b\u7684\u95ee\u9898\u3002\n\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cPython\u5047\u5b9a\u6240\u6709\u6587\u4ef6\u540d\u90fd\u5df2\u7ecf\u6839\u636e sys.getfilesystemencoding() \u7684\u503c\u7f16\u7801\u8fc7\u4e86\u3002\n\u4f46\u662f\uff0c\u6709\u4e00\u4e9b\u6587\u4ef6\u7cfb\u7edf\u5e76\u6ca1\u6709\u5f3a\u5236\u8981\u6c42\u8fd9\u6837\u505a\uff0c\u56e0\u6b64\u5141\u8bb8\u521b\u5efa\u6587\u4ef6\u540d\u6ca1\u6709\u6b63\u786e\u7f16\u7801\u7684\u6587\u4ef6\u3002\n\u8fd9\u79cd\u60c5\u51b5\u4e0d\u592a\u5e38\u89c1\uff0c\u4f46\u662f\u603b\u4f1a\u6709\u4e9b\u7528\u6237\u5192\u9669\u8fd9\u6837\u505a\u6216\u8005\u662f\u65e0\u610f\u4e4b\u4e2d\u8fd9\u6837\u505a\u4e86(\n\u53ef\u80fd\u662f\u5728\u4e00\u4e2a\u6709\u7f3a\u9677\u7684\u4ee3\u7801\u4e2d\u7ed9 open() \u51fd\u6570\u4f20\u9012\u4e86\u4e00\u4e2a\u4e0d\u5408\u89c4\u8303\u7684\u6587\u4ef6\u540d)\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u6267\u884c\u7c7b\u4f3c os.listdir() \u8fd9\u6837\u7684\u51fd\u6570\u65f6\uff0c\u8fd9\u4e9b\u4e0d\u5408\u89c4\u8303\u7684\u6587\u4ef6\u540d\u5c31\u4f1a\u8ba9Python\u9677\u5165\u56f0\u5883\u3002\n\u4e00\u65b9\u9762\uff0c\u5b83\u4e0d\u80fd\u4ec5\u4ec5\u53ea\u662f\u4e22\u5f03\u8fd9\u4e9b\u4e0d\u5408\u683c\u7684\u540d\u5b57\u3002\u800c\u53e6\u4e00\u65b9\u9762\uff0c\u5b83\u53c8\u4e0d\u80fd\u5c06\u8fd9\u4e9b\u6587\u4ef6\u540d\u8f6c\u6362\u4e3a\u6b63\u786e\u7684\u6587\u672c\u5b57\u7b26\u4e32\u3002\nPython\u5bf9\u8fd9\u4e2a\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u662f\u4ece\u6587\u4ef6\u540d\u4e2d\u83b7\u53d6\u672a\u89e3\u7801\u7684\u5b57\u8282\u503c\u6bd4\u5982 \\xhh\n\u5e76\u5c06\u5b83\u6620\u5c04\u6210Unicode\u5b57\u7b26 \\udchh \u8868\u793a\u7684\u6240\u8c13\u7684\u201d\u4ee3\u7406\u7f16\u7801\u201d\u3002\n\u4e0b\u9762\u4e00\u4e2a\u4f8b\u5b50\u6f14\u793a\u4e86\u5f53\u4e00\u4e2a\u4e0d\u5408\u683c\u76ee\u5f55\u5217\u8868\u4e2d\u542b\u6709\u4e00\u4e2a\u6587\u4ef6\u540d\u4e3ab\u00e4d.txt(\u4f7f\u7528Latin-1\u800c\u4e0d\u662fUTF-8\u7f16\u7801)\u65f6\u7684\u6837\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import os\nfiles = os.listdir('.')\nfiles"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u6709\u4ee3\u7801\u9700\u8981\u64cd\u4f5c\u6587\u4ef6\u540d\u6216\u8005\u5c06\u6587\u4ef6\u540d\u4f20\u9012\u7ed9 open() \u8fd9\u6837\u7684\u51fd\u6570\uff0c\u4e00\u5207\u90fd\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002\n\u53ea\u6709\u5f53\u4f60\u60f3\u8981\u8f93\u51fa\u6587\u4ef6\u540d\u65f6\u624d\u4f1a\u78b0\u5230\u4e9b\u9ebb\u70e6(\u6bd4\u5982\u6253\u5370\u8f93\u51fa\u5230\u5c4f\u5e55\u6216\u65e5\u5fd7\u6587\u4ef6\u7b49)\u3002\n\u7279\u522b\u7684\uff0c\u5f53\u4f60\u60f3\u6253\u5370\u4e0a\u9762\u7684\u6587\u4ef6\u540d\u5217\u8868\u65f6\uff0c\u4f60\u7684\u7a0b\u5e8f\u5c31\u4f1a\u5d29\u6e83\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for name in files:\n    print(name)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7a0b\u5e8f\u5d29\u6e83\u7684\u539f\u56e0\u5c31\u662f\u5b57\u7b26 \\udce4 \u662f\u4e00\u4e2a\u975e\u6cd5\u7684Unicode\u5b57\u7b26\u3002\n\u5b83\u5176\u5b9e\u662f\u4e00\u4e2a\u88ab\u79f0\u4e3a\u4ee3\u7406\u5b57\u7b26\u5bf9\u7684\u53cc\u5b57\u7b26\u7ec4\u5408\u7684\u540e\u534a\u90e8\u5206\u3002\n\u7531\u4e8e\u7f3a\u5c11\u4e86\u524d\u534a\u90e8\u5206\uff0c\u56e0\u6b64\u5b83\u662f\u4e2a\u975e\u6cd5\u7684Unicode\u3002\n\u6240\u4ee5\uff0c\u552f\u4e00\u80fd\u6210\u529f\u8f93\u51fa\u7684\u65b9\u6cd5\u5c31\u662f\u5f53\u9047\u5230\u4e0d\u5408\u6cd5\u6587\u4ef6\u540d\u65f6\u91c7\u53d6\u76f8\u5e94\u7684\u8865\u6551\u63aa\u65bd\u3002\n\u6bd4\u5982\u53ef\u4ee5\u5c06\u4e0a\u8ff0\u4ee3\u7801\u4fee\u6539\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for name in files:\ntry:\n    print(name)\nexcept UnicodeEncodeError:\n    print(bad_filename(name))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728 bad_filename() \u51fd\u6570\u4e2d\u600e\u6837\u5904\u7f6e\u53d6\u51b3\u4e8e\u4f60\u81ea\u5df1\u3002\n\u53e6\u5916\u4e00\u4e2a\u9009\u62e9\u5c31\u662f\u901a\u8fc7\u67d0\u79cd\u65b9\u5f0f\u91cd\u65b0\u7f16\u7801\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def bad_filename(filename):\n    temp = filename.encode(sys.getfilesystemencoding(), errors='surrogateescape')\n    return temp.decode('latin-1')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8bd1\u8005\u6ce8:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "surrogateescape:\n\u8fd9\u79cd\u662fPython\u5728\u7edd\u5927\u90e8\u5206\u9762\u5411OS\u7684API\u4e2d\u6240\u4f7f\u7528\u7684\u9519\u8bef\u5904\u7406\u5668\uff0c\n\u5b83\u80fd\u4ee5\u4e00\u79cd\u4f18\u96c5\u7684\u65b9\u5f0f\u5904\u7406\u7531\u64cd\u4f5c\u7cfb\u7edf\u63d0\u4f9b\u7684\u6570\u636e\u7684\u7f16\u7801\u95ee\u9898\u3002\n\u5728\u89e3\u7801\u51fa\u9519\u65f6\u4f1a\u5c06\u51fa\u9519\u5b57\u8282\u5b58\u50a8\u5230\u4e00\u4e2a\u5f88\u5c11\u88ab\u4f7f\u7528\u5230\u7684Unicode\u7f16\u7801\u8303\u56f4\u5185\u3002\n\u5728\u7f16\u7801\u65f6\u5c06\u90a3\u4e9b\u9690\u85cf\u503c\u53c8\u8fd8\u539f\u56de\u539f\u5148\u89e3\u7801\u5931\u8d25\u7684\u5b57\u8282\u5e8f\u5217\u3002\n\u5b83\u4e0d\u4ec5\u5bf9\u4e8eOS API\u975e\u5e38\u6709\u7528\uff0c\u4e5f\u80fd\u5f88\u5bb9\u6613\u7684\u5904\u7406\u5176\u4ed6\u60c5\u51b5\u4e0b\u7684\u7f16\u7801\u9519\u8bef\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u8fd9\u4e2a\u7248\u672c\u4ea7\u751f\u7684\u8f93\u51fa\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for name in files:\n    try:\n        print(name)\n    except UnicodeEncodeError:\n        print(bad_filename(name))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e00\u5c0f\u8282\u4e3b\u9898\u53ef\u80fd\u4f1a\u88ab\u5927\u90e8\u5206\u8bfb\u8005\u6240\u5ffd\u7565\u3002\u4f46\u662f\u5982\u679c\u4f60\u5728\u7f16\u5199\u4f9d\u8d56\u6587\u4ef6\u540d\u548c\u6587\u4ef6\u7cfb\u7edf\u7684\u5173\u952e\u4efb\u52a1\u7a0b\u5e8f\u65f6\uff0c\n\u5c31\u5fc5\u987b\u5f97\u8003\u8651\u5230\u8fd9\u4e2a\u3002\u5426\u5219\u4f60\u53ef\u80fd\u4f1a\u5728\u67d0\u4e2a\u5468\u672b\u88ab\u53eb\u5230\u529e\u516c\u5ba4\u53bb\u8c03\u8bd5\u4e00\u4e9b\u4ee4\u4eba\u8d39\u89e3\u7684\u9519\u8bef\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.16 \u589e\u52a0\u6216\u6539\u53d8\u5df2\u6253\u5f00\u6587\u4ef6\u7684\u7f16\u7801\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5728\u4e0d\u5173\u95ed\u4e00\u4e2a\u5df2\u6253\u5f00\u7684\u6587\u4ef6\u524d\u63d0\u4e0b\u589e\u52a0\u6216\u6539\u53d8\u5b83\u7684Unicode\u7f16\u7801\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u7ed9\u4e00\u4e2a\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u7684\u6587\u4ef6\u6dfb\u52a0Unicode\u7f16\u7801/\u89e3\u7801\u65b9\u5f0f\uff0c\n\u53ef\u4ee5\u4f7f\u7528 io.TextIOWrapper() \u5bf9\u8c61\u5305\u88c5\u5b83\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import urllib.request\nimport io\n\nu = urllib.request.urlopen('http://www.python.org')\nf = io.TextIOWrapper(u, encoding='utf-8')\ntext = f.read()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u4fee\u6539\u4e00\u4e2a\u5df2\u7ecf\u6253\u5f00\u7684\u6587\u672c\u6a21\u5f0f\u7684\u6587\u4ef6\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528 detach() \u65b9\u6cd5\u79fb\u9664\u6389\u5df2\u5b58\u5728\u7684\u6587\u672c\u7f16\u7801\u5c42\uff0c\n\u5e76\u4f7f\u7528\u65b0\u7684\u7f16\u7801\u65b9\u5f0f\u4ee3\u66ff\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5728 sys.stdout \u4e0a\u4fee\u6539\u7f16\u7801\u65b9\u5f0f\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\nsys.stdout.encoding"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='latin-1')\nsys.stdout.encoding"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6837\u505a\u53ef\u80fd\u4f1a\u4e2d\u65ad\u4f60\u7684\u7ec8\u7aef\uff0c\u8fd9\u91cc\u4ec5\u4ec5\u662f\u4e3a\u4e86\u6f14\u793a\u800c\u5df2\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "I/O\u7cfb\u7edf\u7531\u4e00\u7cfb\u5217\u7684\u5c42\u6b21\u6784\u5efa\u800c\u6210\u3002\u4f60\u53ef\u4ee5\u8bd5\u7740\u8fd0\u884c\u4e0b\u9762\u8fd9\u4e2a\u64cd\u4f5c\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u7684\u4f8b\u5b50\u6765\u67e5\u770b\u8fd9\u79cd\u5c42\u6b21\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('sample.txt','w')\nf"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f.buffer"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f.buffer.raw"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cio.TextIOWrapper \u662f\u4e00\u4e2a\u7f16\u7801\u548c\u89e3\u7801Unicode\u7684\u6587\u672c\u5904\u7406\u5c42\uff0c\nio.BufferedWriter \u662f\u4e00\u4e2a\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e\u7684\u5e26\u7f13\u51b2\u7684I/O\u5c42\uff0c\nio.FileIO \u662f\u4e00\u4e2a\u8868\u793a\u64cd\u4f5c\u7cfb\u7edf\u5e95\u5c42\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u539f\u59cb\u6587\u4ef6\u3002\n\u589e\u52a0\u6216\u6539\u53d8\u6587\u672c\u7f16\u7801\u4f1a\u6d89\u53ca\u589e\u52a0\u6216\u6539\u53d8\u6700\u4e0a\u9762\u7684 io.TextIOWrapper \u5c42\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u822c\u6765\u8bb2\uff0c\u50cf\u4e0a\u9762\u4f8b\u5b50\u8fd9\u6837\u901a\u8fc7\u8bbf\u95ee\u5c5e\u6027\u503c\u6765\u76f4\u63a5\u64cd\u4f5c\u4e0d\u540c\u7684\u5c42\u662f\u5f88\u4e0d\u5b89\u5168\u7684\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u8bd5\u7740\u4f7f\u7528\u4e0b\u9762\u8fd9\u6837\u7684\u6280\u672f\u6539\u53d8\u7f16\u7801\u770b\u770b\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = io.TextIOWrapper(f.buffer, encoding='latin-1')\nf"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f.write('Hello')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7ed3\u679c\u51fa\u9519\u4e86\uff0c\u56e0\u4e3af\u7684\u539f\u59cb\u503c\u5df2\u7ecf\u88ab\u7834\u574f\u4e86\u5e76\u5173\u95ed\u4e86\u5e95\u5c42\u7684\u6587\u4ef6\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "detach() \u65b9\u6cd5\u4f1a\u65ad\u5f00\u6587\u4ef6\u7684\u6700\u9876\u5c42\u5e76\u8fd4\u56de\u7b2c\u4e8c\u5c42\uff0c\u4e4b\u540e\u6700\u9876\u5c42\u5c31\u6ca1\u4ec0\u4e48\u7528\u4e86\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('sample.txt', 'w')\nf"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "b = f.detach()\nb"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f.write('hello')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u65e6\u65ad\u5f00\u6700\u9876\u5c42\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u7ed9\u8fd4\u56de\u7ed3\u679c\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u6700\u9876\u5c42\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = io.TextIOWrapper(b, encoding='latin-1')\nf"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u5df2\u7ecf\u5411\u4f60\u6f14\u793a\u4e86\u6539\u53d8\u7f16\u7801\u7684\u65b9\u6cd5\uff0c\n\u4f46\u662f\u4f60\u8fd8\u53ef\u4ee5\u5229\u7528\u8fd9\u79cd\u6280\u672f\u6765\u6539\u53d8\u6587\u4ef6\u884c\u5904\u7406\u3001\u9519\u8bef\u673a\u5236\u4ee5\u53ca\u6587\u4ef6\u5904\u7406\u7684\u5176\u4ed6\u65b9\u9762\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='ascii',\n                            errors='xmlcharrefreplace')\nprint('Jalape\\u00f1o')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6ce8\u610f\u4e0b\u6700\u540e\u8f93\u51fa\u4e2d\u7684\u975eASCII\u5b57\u7b26 \u00f1 \u662f\u5982\u4f55\u88ab &#241; \u53d6\u4ee3\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.17 \u5c06\u5b57\u8282\u5199\u5165\u6587\u672c\u6587\u4ef6\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5728\u6587\u672c\u6a21\u5f0f\u6253\u5f00\u7684\u6587\u4ef6\u4e2d\u5199\u5165\u539f\u59cb\u7684\u5b57\u8282\u6570\u636e\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c06\u5b57\u8282\u6570\u636e\u76f4\u63a5\u5199\u5165\u6587\u4ef6\u7684\u7f13\u51b2\u533a\u5373\u53ef\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\nsys.stdout.write(b'Hello\\n')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "sys.stdout.buffer.write(b'Hello\\n')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7c7b\u4f3c\u7684\uff0c\u80fd\u591f\u901a\u8fc7\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u7684 buffer \u5c5e\u6027\u6765\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6570\u636e\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "I/O\u7cfb\u7edf\u4ee5\u5c42\u7ea7\u7ed3\u6784\u7684\u5f62\u5f0f\u6784\u5efa\u800c\u6210\u3002\n\u6587\u672c\u6587\u4ef6\u662f\u901a\u8fc7\u5728\u4e00\u4e2a\u62e5\u6709\u7f13\u51b2\u7684\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6587\u4ef6\u4e0a\u589e\u52a0\u4e00\u4e2aUnicode\u7f16\u7801/\u89e3\u7801\u5c42\u6765\u521b\u5efa\u3002\nbuffer \u5c5e\u6027\u6307\u5411\u5bf9\u5e94\u7684\u5e95\u5c42\u6587\u4ef6\u3002\u5982\u679c\u4f60\u76f4\u63a5\u8bbf\u95ee\u5b83\u7684\u8bdd\u5c31\u4f1a\u7ed5\u8fc7\u6587\u672c\u7f16\u7801/\u89e3\u7801\u5c42\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u5c0f\u8282\u4f8b\u5b50\u5c55\u793a\u7684 sys.stdout \u53ef\u80fd\u770b\u8d77\u6765\u6709\u70b9\u7279\u6b8a\u3002\n\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0csys.stdout \u603b\u662f\u4ee5\u6587\u672c\u6a21\u5f0f\u6253\u5f00\u7684\u3002\n\u4f46\u662f\u5982\u679c\u4f60\u5728\u5199\u4e00\u4e2a\u9700\u8981\u6253\u5370\u4e8c\u8fdb\u5236\u6570\u636e\u5230\u6807\u51c6\u8f93\u51fa\u7684\u811a\u672c\u7684\u8bdd\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0a\u9762\u6f14\u793a\u7684\u6280\u672f\u6765\u7ed5\u8fc7\u6587\u672c\u7f16\u7801\u5c42\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.18 \u5c06\u6587\u4ef6\u63cf\u8ff0\u7b26\u5305\u88c5\u6210\u6587\u4ef6\u5bf9\u8c61\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6709\u4e00\u4e2a\u5bf9\u5e94\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u4e00\u4e2a\u5df2\u6253\u5f00\u7684I/O\u901a\u9053(\u6bd4\u5982\u6587\u4ef6\u3001\u7ba1\u9053\u3001\u5957\u63a5\u5b57\u7b49)\u7684\u6574\u578b\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\n\u4f60\u60f3\u5c06\u5b83\u5305\u88c5\u6210\u4e00\u4e2a\u66f4\u9ad8\u5c42\u7684Python\u6587\u4ef6\u5bf9\u8c61\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u548c\u4e00\u4e2a\u6253\u5f00\u7684\u666e\u901a\u6587\u4ef6\u662f\u4e0d\u4e00\u6837\u7684\u3002\n\u6587\u4ef6\u63cf\u8ff0\u7b26\u4ec5\u4ec5\u662f\u4e00\u4e2a\u7531\u64cd\u4f5c\u7cfb\u7edf\u6307\u5b9a\u7684\u6574\u6570\uff0c\u7528\u6765\u6307\u4ee3\u67d0\u4e2a\u7cfb\u7edf\u7684I/O\u901a\u9053\u3002\n\u5982\u679c\u4f60\u78b0\u5de7\u6709\u8fd9\u4e48\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 open() \u51fd\u6570\u6765\u5c06\u5176\u5305\u88c5\u4e3a\u4e00\u4e2aPython\u7684\u6587\u4ef6\u5bf9\u8c61\u3002\n\u4f60\u4ec5\u4ec5\u53ea\u9700\u8981\u4f7f\u7528\u8fd9\u4e2a\u6574\u6570\u503c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u6765\u4ee3\u66ff\u6587\u4ef6\u540d\u5373\u53ef\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Open a low-level file descriptor\nimport os\nfd = os.open('somefile.txt', os.O_WRONLY | os.O_CREAT)\n\n# Turn into a proper file\nf = open(fd, 'wt')\nf.write('hello world\\n')\nf.close()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u9ad8\u5c42\u7684\u6587\u4ef6\u5bf9\u8c61\u88ab\u5173\u95ed\u6216\u8005\u7834\u574f\u7684\u65f6\u5019\uff0c\u5e95\u5c42\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e5f\u4f1a\u88ab\u5173\u95ed\u3002\n\u5982\u679c\u8fd9\u4e2a\u5e76\u4e0d\u662f\u4f60\u60f3\u8981\u7684\u7ed3\u679c\uff0c\u4f60\u53ef\u4ee5\u7ed9 open() \u51fd\u6570\u4f20\u9012\u4e00\u4e2a\u53ef\u9009\u7684 colsefd=False \u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Create a file object, but don't close underlying fd when done\nf = open(fd, 'wt', closefd=False)\n..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728Unix\u7cfb\u7edf\u4e2d\uff0c\u8fd9\u79cd\u5305\u88c5\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u6280\u672f\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u5c06\u4e00\u4e2a\u7c7b\u6587\u4ef6\u63a5\u53e3\u4f5c\u7528\u4e8e\u4e00\u4e2a\u4ee5\u4e0d\u540c\u65b9\u5f0f\u6253\u5f00\u7684I/O\u901a\u9053\u4e0a\uff0c\n\u5982\u7ba1\u9053\u3001\u5957\u63a5\u5b57\u7b49\u3002\u4e3e\u4f8b\u6765\u8bb2\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u64cd\u4f5c\u7ba1\u9053\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from socket import socket, AF_INET, SOCK_STREAM\n\ndef echo_client(client_sock, addr):\n    print('Got connection from', addr)\n\n    # Make text-mode file wrappers for socket reading/writing\n    client_in = open(client_sock.fileno(), 'rt', encoding='latin-1',\n                closefd=False)\n\n    client_out = open(client_sock.fileno(), 'wt', encoding='latin-1',\n                closefd=False)\n\n    # Echo lines back to the client using file I/O\n    for line in client_in:\n        client_out.write(line)\n        client_out.flush()\n\n    client_sock.close()\n\ndef echo_server(address):\n    sock = socket(AF_INET, SOCK_STREAM)\n    sock.bind(address)\n    sock.listen(1)\n    while True:\n        client, addr = sock.accept()\n        echo_client(client, addr)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9700\u8981\u91cd\u70b9\u5f3a\u8c03\u7684\u4e00\u70b9\u662f\uff0c\u4e0a\u9762\u7684\u4f8b\u5b50\u4ec5\u4ec5\u662f\u4e3a\u4e86\u6f14\u793a\u5185\u7f6e\u7684 open() \u51fd\u6570\u7684\u4e00\u4e2a\u7279\u6027\uff0c\u5e76\u4e14\u4e5f\u53ea\u9002\u7528\u4e8e\u57fa\u4e8eUnix\u7684\u7cfb\u7edf\u3002\n\u5982\u679c\u4f60\u60f3\u5c06\u4e00\u4e2a\u7c7b\u6587\u4ef6\u63a5\u53e3\u4f5c\u7528\u5728\u4e00\u4e2a\u5957\u63a5\u5b57\u5e76\u5e0c\u671b\u4f60\u7684\u4ee3\u7801\u53ef\u4ee5\u8de8\u5e73\u53f0\uff0c\u8bf7\u4f7f\u7528\u5957\u63a5\u5b57\u5bf9\u8c61\u7684 makefile() \u65b9\u6cd5\u3002\n\u4f46\u662f\u5982\u679c\u4e0d\u8003\u8651\u53ef\u79fb\u690d\u6027\u7684\u8bdd\uff0c\u90a3\u4e0a\u9762\u7684\u89e3\u51b3\u65b9\u6848\u4f1a\u6bd4\u4f7f\u7528 makefile() \u6027\u80fd\u66f4\u597d\u4e00\u70b9\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u8fd9\u79cd\u6280\u672f\u6765\u6784\u9020\u4e00\u4e2a\u522b\u540d\uff0c\u5141\u8bb8\u4ee5\u4e0d\u540c\u4e8e\u7b2c\u4e00\u6b21\u6253\u5f00\u6587\u4ef6\u7684\u65b9\u5f0f\u4f7f\u7528\u5b83\u3002\n\u4f8b\u5982\uff0c\u4e0b\u9762\u6f14\u793a\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61\uff0c\u5b83\u5141\u8bb8\u4f60\u8f93\u51fa\u4e8c\u8fdb\u5236\u6570\u636e\u5230\u6807\u51c6\u8f93\u51fa(\u901a\u5e38\u4ee5\u6587\u672c\u6a21\u5f0f\u6253\u5f00)\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\n# Create a binary-mode file for stdout\nbstdout = open(sys.stdout.fileno(), 'wb', closefd=False)\nbstdout.write(b'Hello World\\n')\nbstdout.flush()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u5305\u88c5\u6210\u4e00\u4e2a\u6b63\u5e38\u7684\u6587\u4ef6\u5bf9\u8c61\uff0c\n\u4f46\u662f\u8981\u6ce8\u610f\u7684\u662f\u5e76\u4e0d\u662f\u6240\u6709\u7684\u6587\u4ef6\u6a21\u5f0f\u90fd\u88ab\u652f\u6301\uff0c\u5e76\u4e14\u67d0\u4e9b\u7c7b\u578b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ef\u80fd\u4f1a\u6709\u526f\u4f5c\u7528\n(\u7279\u522b\u662f\u6d89\u53ca\u5230\u9519\u8bef\u5904\u7406\u3001\u6587\u4ef6\u7ed3\u5c3e\u6761\u4ef6\u7b49\u7b49\u7684\u65f6\u5019)\u3002\n\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd9\u79cd\u884c\u4e3a\u4e5f\u662f\u4e0d\u4e00\u6837\uff0c\u7279\u522b\u7684\uff0c\u4e0a\u9762\u7684\u4f8b\u5b50\u90fd\u4e0d\u80fd\u5728\u975eUnix\u7cfb\u7edf\u4e0a\u8fd0\u884c\u3002\n\u6211\u8bf4\u4e86\u8fd9\u4e48\u591a\uff0c\u610f\u601d\u5c31\u662f\u8ba9\u4f60\u5145\u5206\u6d4b\u8bd5\u81ea\u5df1\u7684\u5b9e\u73b0\u4ee3\u7801\uff0c\u786e\u4fdd\u5b83\u80fd\u6309\u7167\u671f\u671b\u5de5\u4f5c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.19 \u521b\u5efa\u4e34\u65f6\u6587\u4ef6\u548c\u6587\u4ef6\u5939\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u9700\u8981\u5728\u7a0b\u5e8f\u6267\u884c\u65f6\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6587\u4ef6\u6216\u76ee\u5f55\uff0c\u5e76\u5e0c\u671b\u4f7f\u7528\u5b8c\u4e4b\u540e\u53ef\u4ee5\u81ea\u52a8\u9500\u6bc1\u6389\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "tempfile \u6a21\u5757\u4e2d\u6709\u5f88\u591a\u7684\u51fd\u6570\u53ef\u4ee5\u5b8c\u6210\u8fd9\u4efb\u52a1\u3002\n\u4e3a\u4e86\u521b\u5efa\u4e00\u4e2a\u533f\u540d\u7684\u4e34\u65f6\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528 tempfile.TemporaryFile \uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from tempfile import TemporaryFile\n\nwith TemporaryFile('w+t') as f:\n    # Read/write to the file\n    f.write('Hello World\\n')\n    f.write('Testing\\n')\n\n    # Seek back to beginning and read the data\n    f.seek(0)\n    data = f.read()\n\n# Temporary file is destroyed"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6216\u8005\uff0c\u5982\u679c\u4f60\u559c\u6b22\uff0c\u4f60\u8fd8\u53ef\u4ee5\u50cf\u8fd9\u6837\u4f7f\u7528\u4e34\u65f6\u6587\u4ef6\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = TemporaryFile('w+t')\n# Use the temporary file\n...\nf.close()\n# File is destroyed"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "TemporaryFile() \u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6587\u4ef6\u6a21\u5f0f\uff0c\u901a\u5e38\u6765\u8bb2\u6587\u672c\u6a21\u5f0f\u4f7f\u7528 w+t \uff0c\u4e8c\u8fdb\u5236\u6a21\u5f0f\u4f7f\u7528 w+b \u3002\n\u8fd9\u4e2a\u6a21\u5f0f\u540c\u65f6\u652f\u6301\u8bfb\u548c\u5199\u64cd\u4f5c\uff0c\u5728\u8fd9\u91cc\u662f\u5f88\u6709\u7528\u7684\uff0c\u56e0\u4e3a\u5f53\u4f60\u5173\u95ed\u6587\u4ef6\u53bb\u6539\u53d8\u6a21\u5f0f\u7684\u65f6\u5019\uff0c\u6587\u4ef6\u5b9e\u9645\u4e0a\u5df2\u7ecf\u4e0d\u5b58\u5728\u4e86\u3002\nTemporaryFile() \u53e6\u5916\u8fd8\u652f\u6301\u8ddf\u5185\u7f6e\u7684 open() \u51fd\u6570\u4e00\u6837\u7684\u53c2\u6570\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with TemporaryFile('w+t', encoding='utf-8', errors='ignore') as f:\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u5927\u591a\u6570Unix\u7cfb\u7edf\u4e0a\uff0c\u901a\u8fc7 TemporaryFile() \u521b\u5efa\u7684\u6587\u4ef6\u90fd\u662f\u533f\u540d\u7684\uff0c\u751a\u81f3\u8fde\u76ee\u5f55\u90fd\u6ca1\u6709\u3002\n\u5982\u679c\u4f60\u60f3\u6253\u7834\u8fd9\u4e2a\u9650\u5236\uff0c\u53ef\u4ee5\u4f7f\u7528 NamedTemporaryFile() \u6765\u4ee3\u66ff\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from tempfile import NamedTemporaryFile\n\nwith NamedTemporaryFile('w+t') as f:\n    print('filename is:', f.name)\n    ...\n\n# File automatically destroyed"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u91cc\uff0c\u88ab\u6253\u5f00\u6587\u4ef6\u7684 f.name \u5c5e\u6027\u5305\u542b\u4e86\u8be5\u4e34\u65f6\u6587\u4ef6\u7684\u6587\u4ef6\u540d\u3002\n\u5f53\u4f60\u9700\u8981\u5c06\u6587\u4ef6\u540d\u4f20\u9012\u7ed9\u5176\u4ed6\u4ee3\u7801\u6765\u6253\u5f00\u8fd9\u4e2a\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u5c31\u5f88\u6709\u7528\u4e86\u3002\n\u548c TemporaryFile() \u4e00\u6837\uff0c\u7ed3\u679c\u6587\u4ef6\u5173\u95ed\u65f6\u4f1a\u88ab\u81ea\u52a8\u5220\u9664\u6389\u3002\n\u5982\u679c\u4f60\u4e0d\u60f3\u8fd9\u4e48\u505a\uff0c\u53ef\u4ee5\u4f20\u9012\u4e00\u4e2a\u5173\u952e\u5b57\u53c2\u6570 delete=False \u5373\u53ef\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with NamedTemporaryFile('w+t', delete=False) as f:\n    print('filename is:', f.name)\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528 tempfile.TemporaryDirectory() \u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from tempfile import TemporaryDirectory\n\nwith TemporaryDirectory() as dirname:\n    print('dirname is:', dirname)\n    # Use the directory\n    ...\n# Directory and all contents destroyed"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "TemporaryFile() \u3001NamedTemporaryFile() \u548c TemporaryDirectory() \u51fd\u6570\n\u5e94\u8be5\u662f\u5904\u7406\u4e34\u65f6\u6587\u4ef6\u76ee\u5f55\u7684\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u4e86\uff0c\u56e0\u4e3a\u5b83\u4eec\u4f1a\u81ea\u52a8\u5904\u7406\u6240\u6709\u7684\u521b\u5efa\u548c\u6e05\u7406\u6b65\u9aa4\u3002\n\u5728\u4e00\u4e2a\u66f4\u4f4e\u7684\u7ea7\u522b\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 mkstemp() \u548c mkdtemp() \u6765\u521b\u5efa\u4e34\u65f6\u6587\u4ef6\u548c\u76ee\u5f55\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import tempfile\ntempfile.mkstemp()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "tempfile.mkdtemp()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f46\u662f\uff0c\u8fd9\u4e9b\u51fd\u6570\u5e76\u4e0d\u4f1a\u505a\u8fdb\u4e00\u6b65\u7684\u7ba1\u7406\u4e86\u3002\n\u4f8b\u5982\uff0c\u51fd\u6570 mkstemp() \u4ec5\u4ec5\u5c31\u8fd4\u56de\u4e00\u4e2a\u539f\u59cb\u7684OS\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u4f60\u9700\u8981\u81ea\u5df1\u5c06\u5b83\u8f6c\u6362\u4e3a\u4e00\u4e2a\u771f\u6b63\u7684\u6587\u4ef6\u5bf9\u8c61\u3002\n\u540c\u6837\u4f60\u8fd8\u9700\u8981\u81ea\u5df1\u6e05\u7406\u8fd9\u4e9b\u6587\u4ef6\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u5e38\u6765\u8bb2\uff0c\u4e34\u65f6\u6587\u4ef6\u5728\u7cfb\u7edf\u9ed8\u8ba4\u7684\u4f4d\u7f6e\u88ab\u521b\u5efa\uff0c\u6bd4\u5982 /var/tmp \u6216\u7c7b\u4f3c\u7684\u5730\u65b9\u3002\n\u4e3a\u4e86\u83b7\u53d6\u771f\u5b9e\u7684\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528 tempfile.gettempdir() \u51fd\u6570\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "tempfile.gettempdir()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6240\u6709\u548c\u4e34\u65f6\u6587\u4ef6\u76f8\u5173\u7684\u51fd\u6570\u90fd\u5141\u8bb8\u4f60\u901a\u8fc7\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\nprefix \u3001suffix \u548c dir \u6765\u81ea\u5b9a\u4e49\u76ee\u5f55\u4ee5\u53ca\u547d\u540d\u89c4\u5219\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = NamedTemporaryFile(prefix='mytemp', suffix='.txt', dir='/tmp')\nf.name"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u8fd8\u6709\u4e00\u70b9\uff0c\u5c3d\u53ef\u80fd\u4ee5\u6700\u5b89\u5168\u7684\u65b9\u5f0f\u4f7f\u7528 tempfile \u6a21\u5757\u6765\u521b\u5efa\u4e34\u65f6\u6587\u4ef6\u3002\n\u5305\u62ec\u4ec5\u7ed9\u5f53\u524d\u7528\u6237\u6388\u6743\u8bbf\u95ee\u4ee5\u53ca\u5728\u6587\u4ef6\u521b\u5efa\u8fc7\u7a0b\u4e2d\u91c7\u53d6\u63aa\u65bd\u907f\u514d\u7ade\u6001\u6761\u4ef6\u3002\n\u8981\u6ce8\u610f\u7684\u662f\u4e0d\u540c\u7684\u5e73\u53f0\u53ef\u80fd\u4f1a\u4e0d\u4e00\u6837\u3002\u56e0\u6b64\u4f60\u6700\u597d\u9605\u8bfb\n\u5b98\u65b9\u6587\u6863 \u6765\u4e86\u89e3\u66f4\u591a\u7684\u7ec6\u8282\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.20 \u4e0e\u4e32\u884c\u7aef\u53e3\u7684\u6570\u636e\u901a\u4fe1\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u901a\u8fc7\u4e32\u884c\u7aef\u53e3\u8bfb\u5199\u6570\u636e\uff0c\u5178\u578b\u573a\u666f\u5c31\u662f\u548c\u4e00\u4e9b\u786c\u4ef6\u8bbe\u5907\u6253\u4ea4\u9053(\u6bd4\u5982\u4e00\u4e2a\u673a\u5668\u4eba\u6216\u4f20\u611f\u5668)\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Python\u5185\u7f6e\u7684I/O\u6a21\u5757\u6765\u5b8c\u6210\u8fd9\u4e2a\u4efb\u52a1\uff0c\u4f46\u5bf9\u4e8e\u4e32\u884c\u901a\u4fe1\u6700\u597d\u7684\u9009\u62e9\u662f\u4f7f\u7528\npySerial\u5305 \u3002\n\u8fd9\u4e2a\u5305\u7684\u4f7f\u7528\u975e\u5e38\u7b80\u5355\uff0c\u5148\u5b89\u88c5pySerial\uff0c\u4f7f\u7528\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\u7684\u4ee3\u7801\u5c31\u80fd\u5f88\u5bb9\u6613\u7684\u6253\u5f00\u4e00\u4e2a\u4e32\u884c\u7aef\u53e3\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import serial\nser = serial.Serial('/dev/tty.usbmodem641', # Device name varies\n                    baudrate=9600,\n                    bytesize=8,\n                    parity='N',\n                    stopbits=1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8bbe\u5907\u540d\u5bf9\u4e8e\u4e0d\u540c\u7684\u8bbe\u5907\u548c\u64cd\u4f5c\u7cfb\u7edf\u662f\u4e0d\u4e00\u6837\u7684\u3002\n\u6bd4\u5982\uff0c\u5728Windows\u7cfb\u7edf\u4e0a\uff0c\u4f60\u53ef\u4ee5\u4f7f\u75280, 1\u7b49\u8868\u793a\u7684\u4e00\u4e2a\u8bbe\u5907\u6765\u6253\u5f00\u901a\u4fe1\u7aef\u53e3\u201dCOM0\u201d\u548c\u201dCOM1\u201d\u3002\n\u4e00\u65e6\u7aef\u53e3\u6253\u5f00\uff0c\u90a3\u5c31\u53ef\u4ee5\u4f7f\u7528 read()\uff0creadline() \u548c write() \u51fd\u6570\u8bfb\u5199\u6570\u636e\u4e86\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "ser.write(b'G1 X50 Y50\\r\\n')\nresp = ser.readline()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u7b80\u5355\u7684\u4e32\u53e3\u901a\u4fe1\u4ece\u6b64\u53d8\u5f97\u5341\u5206\u7b80\u5355\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u8868\u9762\u4e0a\u770b\u8d77\u6765\u5f88\u7b80\u5355\uff0c\u5176\u5b9e\u4e32\u53e3\u901a\u4fe1\u6709\u65f6\u5019\u4e5f\u662f\u633a\u9ebb\u70e6\u7684\u3002\n\u63a8\u8350\u4f60\u4f7f\u7528\u7b2c\u4e09\u65b9\u5305\u5982 pySerial \u7684\u4e00\u4e2a\u539f\u56e0\u662f\u5b83\u63d0\u4f9b\u4e86\u5bf9\u9ad8\u7ea7\u7279\u6027\u7684\u652f\u6301\n(\u6bd4\u5982\u8d85\u65f6\uff0c\u63a7\u5236\u6d41\uff0c\u7f13\u51b2\u533a\u5237\u65b0\uff0c\u63e1\u624b\u534f\u8bae\u7b49\u7b49)\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u4f60\u60f3\u542f\u7528 RTS-CTS \u63e1\u624b\u534f\u8bae\uff0c\n\u4f60\u53ea\u9700\u8981\u7ed9 Serial() \u4f20\u9012\u4e00\u4e2a rtscts=True \u7684\u53c2\u6570\u5373\u53ef\u3002\n\u5176\u5b98\u65b9\u6587\u6863\u975e\u5e38\u5b8c\u5584\uff0c\u56e0\u6b64\u6211\u5728\u8fd9\u91cc\u6781\u529b\u63a8\u8350\u8fd9\u4e2a\u5305\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u65f6\u523b\u8bb0\u4f4f\u6240\u6709\u6d89\u53ca\u5230\u4e32\u53e3\u7684I/O\u90fd\u662f\u4e8c\u8fdb\u5236\u6a21\u5f0f\u7684\u3002\u56e0\u6b64\uff0c\u786e\u4fdd\u4f60\u7684\u4ee3\u7801\u4f7f\u7528\u7684\u662f\u5b57\u8282\u800c\u4e0d\u662f\u6587\u672c\n(\u6216\u6709\u65f6\u5019\u6267\u884c\u6587\u672c\u7684\u7f16\u7801/\u89e3\u7801\u64cd\u4f5c)\u3002\n\u53e6\u5916\u5f53\u4f60\u9700\u8981\u521b\u5efa\u4e8c\u8fdb\u5236\u7f16\u7801\u7684\u6307\u4ee4\u6216\u6570\u636e\u5305\u7684\u65f6\u5019\uff0cstruct \u6a21\u5757\u4e5f\u662f\u975e\u5e38\u6709\u7528\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.21 \u5e8f\u5217\u5316Python\u5bf9\u8c61\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u9700\u8981\u5c06\u4e00\u4e2aPython\u5bf9\u8c61\u5e8f\u5217\u5316\u4e3a\u4e00\u4e2a\u5b57\u8282\u6d41\uff0c\u4ee5\u4fbf\u5c06\u5b83\u4fdd\u5b58\u5230\u4e00\u4e2a\u6587\u4ef6\u3001\u5b58\u50a8\u5230\u6570\u636e\u5e93\u6216\u8005\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\u5b83\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u5e8f\u5217\u5316\u6700\u666e\u904d\u7684\u505a\u6cd5\u5c31\u662f\u4f7f\u7528 pickle \u6a21\u5757\u3002\u4e3a\u4e86\u5c06\u4e00\u4e2a\u5bf9\u8c61\u4fdd\u5b58\u5230\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import pickle\n\ndata = ... # Some Python object\nf = open('somefile', 'wb')\npickle.dump(data, f)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u5c06\u4e00\u4e2a\u5bf9\u8c61\u8f6c\u50a8\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528 pickle.dumps() \uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = pickle.dumps(data)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u4ece\u5b57\u8282\u6d41\u4e2d\u6062\u590d\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4f7f\u7528 pickle.load() \u6216 pickle.loads() \u51fd\u6570\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Restore from a file\nf = open('somefile', 'rb')\ndata = pickle.load(f)\n\n# Restore from a string\ndata = pickle.loads(s)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u5927\u591a\u6570\u5e94\u7528\u7a0b\u5e8f\u6765\u8bb2\uff0cdump() \u548c load() \u51fd\u6570\u7684\u4f7f\u7528\u5c31\u662f\u4f60\u6709\u6548\u4f7f\u7528 pickle \u6a21\u5757\u6240\u9700\u7684\u5168\u90e8\u4e86\u3002\n\u5b83\u53ef\u9002\u7528\u4e8e\u7edd\u5927\u90e8\u5206Python\u6570\u636e\u7c7b\u578b\u548c\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u7684\u5bf9\u8c61\u5b9e\u4f8b\u3002\n\u5982\u679c\u4f60\u78b0\u5230\u67d0\u4e2a\u5e93\u53ef\u4ee5\u8ba9\u4f60\u5728\u6570\u636e\u5e93\u4e2d\u4fdd\u5b58/\u6062\u590dPython\u5bf9\u8c61\u6216\u8005\u662f\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\u5bf9\u8c61\u7684\u8bdd\uff0c\n\u90a3\u4e48\u5f88\u6709\u53ef\u80fd\u8fd9\u4e2a\u5e93\u7684\u5e95\u5c42\u5c31\u4f7f\u7528\u4e86 pickle \u6a21\u5757\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "pickle \u662f\u4e00\u79cdPython\u7279\u6709\u7684\u81ea\u63cf\u8ff0\u7684\u6570\u636e\u7f16\u7801\u3002\n\u901a\u8fc7\u81ea\u63cf\u8ff0\uff0c\u88ab\u5e8f\u5217\u5316\u540e\u7684\u6570\u636e\u5305\u542b\u6bcf\u4e2a\u5bf9\u8c61\u5f00\u59cb\u548c\u7ed3\u675f\u4ee5\u53ca\u5b83\u7684\u7c7b\u578b\u4fe1\u606f\u3002\n\u56e0\u6b64\uff0c\u4f60\u65e0\u9700\u62c5\u5fc3\u5bf9\u8c61\u8bb0\u5f55\u7684\u5b9a\u4e49\uff0c\u5b83\u603b\u662f\u80fd\u5de5\u4f5c\u3002\n\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u8981\u5904\u7406\u591a\u4e2a\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import pickle\nf = open('somedata', 'wb')\npickle.dump([1, 2, 3, 4], f)\npickle.dump('hello', f)\npickle.dump({'Apple', 'Pear', 'Banana'}, f)\nf.close()\nf = open('somedata', 'rb')\npickle.load(f)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "pickle.load(f)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "pickle.load(f)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u8fd8\u80fd\u5e8f\u5217\u5316\u51fd\u6570\uff0c\u7c7b\uff0c\u8fd8\u6709\u63a5\u53e3\uff0c\u4f46\u662f\u7ed3\u679c\u6570\u636e\u4ec5\u4ec5\u5c06\u5b83\u4eec\u7684\u540d\u79f0\u7f16\u7801\u6210\u5bf9\u5e94\u7684\u4ee3\u7801\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import math\nimport pickle.\npickle.dumps(math.cos)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u6570\u636e\u53cd\u5e8f\u5217\u5316\u56de\u6765\u7684\u65f6\u5019\uff0c\u4f1a\u5148\u5047\u5b9a\u6240\u6709\u7684\u6e90\u6570\u636e\u65f6\u53ef\u7528\u7684\u3002\n\u6a21\u5757\u3001\u7c7b\u548c\u51fd\u6570\u4f1a\u81ea\u52a8\u6309\u9700\u5bfc\u5165\u8fdb\u6765\u3002\u5bf9\u4e8ePython\u6570\u636e\u88ab\u4e0d\u540c\u673a\u5668\u4e0a\u7684\u89e3\u6790\u5668\u6240\u5171\u4eab\u7684\u5e94\u7528\u7a0b\u5e8f\u800c\u8a00\uff0c\n\u6570\u636e\u7684\u4fdd\u5b58\u53ef\u80fd\u4f1a\u6709\u95ee\u9898\uff0c\u56e0\u4e3a\u6240\u6709\u7684\u673a\u5668\u90fd\u5fc5\u987b\u8bbf\u95ee\u540c\u4e00\u4e2a\u6e90\u4ee3\u7801\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6ce8"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "\u5343\u4e07\u4e0d\u8981\u5bf9\u4e0d\u4fe1\u4efb\u7684\u6570\u636e\u4f7f\u7528pickle.load()\u3002\npickle\u5728\u52a0\u8f7d\u65f6\u6709\u4e00\u4e2a\u526f\u4f5c\u7528\u5c31\u662f\u5b83\u4f1a\u81ea\u52a8\u52a0\u8f7d\u76f8\u5e94\u6a21\u5757\u5e76\u6784\u9020\u5b9e\u4f8b\u5bf9\u8c61\u3002\n\u4f46\u662f\u67d0\u4e2a\u574f\u4eba\u5982\u679c\u77e5\u9053pickle\u7684\u5de5\u4f5c\u539f\u7406\uff0c\n\u4ed6\u5c31\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6076\u610f\u7684\u6570\u636e\u5bfc\u81f4Python\u6267\u884c\u968f\u610f\u6307\u5b9a\u7684\u7cfb\u7edf\u547d\u4ee4\u3002\n\u56e0\u6b64\uff0c\u4e00\u5b9a\u8981\u4fdd\u8bc1pickle\u53ea\u5728\u76f8\u4e92\u4e4b\u95f4\u53ef\u4ee5\u8ba4\u8bc1\u5bf9\u65b9\u7684\u89e3\u6790\u5668\u7684\u5185\u90e8\u4f7f\u7528\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6709\u4e9b\u7c7b\u578b\u7684\u5bf9\u8c61\u662f\u4e0d\u80fd\u88ab\u5e8f\u5217\u5316\u7684\u3002\u8fd9\u4e9b\u901a\u5e38\u662f\u90a3\u4e9b\u4f9d\u8d56\u5916\u90e8\u7cfb\u7edf\u72b6\u6001\u7684\u5bf9\u8c61\uff0c\n\u6bd4\u5982\u6253\u5f00\u7684\u6587\u4ef6\uff0c\u7f51\u7edc\u8fde\u63a5\uff0c\u7ebf\u7a0b\uff0c\u8fdb\u7a0b\uff0c\u6808\u5e27\u7b49\u7b49\u3002\n\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u53ef\u4ee5\u901a\u8fc7\u63d0\u4f9b __getstate__() \u548c __setstate__() \u65b9\u6cd5\u6765\u7ed5\u8fc7\u8fd9\u4e9b\u9650\u5236\u3002\n\u5982\u679c\u5b9a\u4e49\u4e86\u8fd9\u4e24\u4e2a\u65b9\u6cd5\uff0cpickle.dump() \u5c31\u4f1a\u8c03\u7528 __getstate__() \u83b7\u53d6\u5e8f\u5217\u5316\u7684\u5bf9\u8c61\u3002\n\u7c7b\u4f3c\u7684\uff0c__setstate__() \u5728\u53cd\u5e8f\u5217\u5316\u65f6\u88ab\u8c03\u7528\u3002\u4e3a\u4e86\u6f14\u793a\u8fd9\u4e2a\u5de5\u4f5c\u539f\u7406\uff0c\n\u4e0b\u9762\u662f\u4e00\u4e2a\u5728\u5185\u90e8\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u4f46\u4ecd\u7136\u53ef\u4ee5\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u7684\u7c7b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# countdown.py\nimport time\nimport threading\n\nclass Countdown:\n    def __init__(self, n):\n        self.n = n\n        self.thr = threading.Thread(target=self.run)\n        self.thr.daemon = True\n        self.thr.start()\n\n    def run(self):\n        while self.n > 0:\n            print('T-minus', self.n)\n            self.n -= 1\n            time.sleep(5)\n\n    def __getstate__(self):\n        return self.n\n\n    def __setstate__(self, n):\n        self.__init__(n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8bd5\u7740\u8fd0\u884c\u4e0b\u9762\u7684\u5e8f\u5217\u5316\u8bd5\u9a8c\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import countdown\nc = countdown.Countdown(30)\nT-minus 30"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# After a few moments\nf = open('cstate.p', 'wb')\nimport pickle\npickle.dump(c, f)\nf.close()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7136\u540e\u9000\u51faPython\u89e3\u6790\u5668\u5e76\u91cd\u542f\u540e\u518d\u8bd5\u9a8c\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('cstate.p', 'rb')\npickle.load(f)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u53ef\u4ee5\u770b\u5230\u7ebf\u7a0b\u53c8\u5947\u8ff9\u822c\u7684\u91cd\u751f\u4e86\uff0c\u4ece\u4f60\u7b2c\u4e00\u6b21\u5e8f\u5217\u5316\u5b83\u7684\u5730\u65b9\u53c8\u6062\u590d\u8fc7\u6765\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "pickle \u5bf9\u4e8e\u5927\u578b\u7684\u6570\u636e\u7ed3\u6784\u6bd4\u5982\u4f7f\u7528 array \u6216 numpy\n\u6a21\u5757\u521b\u5efa\u7684\u4e8c\u8fdb\u5236\u6570\u7ec4\u6548\u7387\u5e76\u4e0d\u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u7f16\u7801\u65b9\u5f0f\u3002\n\u5982\u679c\u4f60\u9700\u8981\u79fb\u52a8\u5927\u91cf\u7684\u6570\u7ec4\u6570\u636e\uff0c\u4f60\u6700\u597d\u662f\u5148\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u5c06\u5176\u4fdd\u5b58\u4e3a\u6570\u7ec4\u6570\u636e\u5757\u6216\u4f7f\u7528\u66f4\u9ad8\u7ea7\u7684\u6807\u51c6\u7f16\u7801\u65b9\u5f0f\u5982HDF5\n(\u9700\u8981\u7b2c\u4e09\u65b9\u5e93\u7684\u652f\u6301)\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7531\u4e8e pickle \u662fPython\u7279\u6709\u7684\u5e76\u4e14\u9644\u7740\u5728\u6e90\u7801\u4e0a\uff0c\u6240\u6709\u5982\u679c\u9700\u8981\u957f\u671f\u5b58\u50a8\u6570\u636e\u7684\u65f6\u5019\u4e0d\u5e94\u8be5\u9009\u7528\u5b83\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u6e90\u7801\u53d8\u52a8\u4e86\uff0c\u4f60\u6240\u6709\u7684\u5b58\u50a8\u6570\u636e\u53ef\u80fd\u4f1a\u88ab\u7834\u574f\u5e76\u4e14\u53d8\u5f97\u4e0d\u53ef\u8bfb\u53d6\u3002\n\u5766\u767d\u6765\u8bb2\uff0c\u5bf9\u4e8e\u5728\u6570\u636e\u5e93\u548c\u5b58\u6863\u6587\u4ef6\u4e2d\u5b58\u50a8\u6570\u636e\u65f6\uff0c\u4f60\u6700\u597d\u4f7f\u7528\u66f4\u52a0\u6807\u51c6\u7684\u6570\u636e\u7f16\u7801\u683c\u5f0f\u5982XML\uff0cCSV\u6216JSON\u3002\n\u8fd9\u4e9b\u7f16\u7801\u683c\u5f0f\u66f4\u6807\u51c6\uff0c\u53ef\u4ee5\u88ab\u4e0d\u540c\u7684\u8bed\u8a00\u652f\u6301\uff0c\u5e76\u4e14\u4e5f\u80fd\u5f88\u597d\u7684\u9002\u5e94\u6e90\u7801\u53d8\u66f4\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u4e00\u70b9\u8981\u6ce8\u610f\u7684\u662f  pickle \u6709\u5927\u91cf\u7684\u914d\u7f6e\u9009\u9879\u548c\u4e00\u4e9b\u68d8\u624b\u7684\u95ee\u9898\u3002\n\u5bf9\u4e8e\u6700\u5e38\u89c1\u7684\u4f7f\u7528\u573a\u666f\uff0c\u4f60\u4e0d\u9700\u8981\u53bb\u62c5\u5fc3\u8fd9\u4e2a\uff0c\u4f46\u662f\u5982\u679c\u4f60\u8981\u5728\u4e00\u4e2a\u91cd\u8981\u7684\u7a0b\u5e8f\u4e2d\u4f7f\u7528pickle\u53bb\u505a\u5e8f\u5217\u5316\u7684\u8bdd\uff0c\n\u6700\u597d\u53bb\u67e5\u9605\u4e00\u4e0b \u5b98\u65b9\u6587\u6863 \u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}